PurchasesDeal = new Object();

Ext.define('Collateral', {
	extend : 'Ext.data.Model',
	fields : [ {
		name : 'instrumentType',
		type : 'string'
	}, {
		name : 'instrumentNo',
		type : 'string'
	}, {
		name : 'value',
		type : 'float'
	}, {
		name : 'couponDate',
		type : 'date',
		dateFormat : 'd/m/Y'
	} ]
});

var instrumentTypes = Ext.create('Ext.data.Store', {
	fields : [ 'type', 'issuer', 'interest', 'withholdTax', 'coupon', 'couponDates', 'couponFreq', 'dayBasis', 'script', 'denominal', 'listed' ]
});

Ext.define('Instrument', {
	extend : 'Ext.data.Model',
	fields : [ {
		name : 'code',
		type : 'string'
	}, {
		name : 'type',
		type : 'string'
	}, {
		name : 'name',
		type : 'string'
	}, {
		name : 'actualRate',
		type : 'float'
	}, {
		name : 'valueDate',
		type : 'date',
		dateFormat : 'd/m/Y'
	}, {
		name : 'maturityDate',
		type : 'date',
		dateFormat : 'd/m/Y'
	}, {
		name : 'issuer',
		type : 'string'
	} ]
});

var instrumentStore = new Ext.data.Store({
	model : 'Instrument'
});

var benchMarkStore = Ext.create('Ext.data.Store', {
	fields : [ 'name' ],
	data : [ {
		"name" : "BenchMark 1"
	}, {
		"name" : "BenchMark 2"
	}, {
		"name" : "BenchMark 3"
	}, {
		"name" : "BenchMark 4"
	} ]
});

// cpnDtStore.add({couponDate: '06 10 2014'});

PurchasesDeal.getBasicFieldSet = function() {

	var funds = Ext.create('Ext.data.Store', {
		fields : [ {
			name : 'id',
			type : 'int'
		}, {
			name : 'fundName',
			type : 'string'
		},// 'id', 'fundName'
		{
			name : 'fundCode',
			type : 'string'
		} ],
		storeId : 'dealFundsStore'
	});

	return Ext.create('Ext.form.FieldSet', {
		title : 'Basic',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'label',
			id : 'recPurchParamLbl',
			hidden : true,
			text : ''
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ Ext.create('Ext.form.ComboBox', {
				name : 'fund',
				fieldLabel : 'Fund',
				store : funds,
				queryMode : 'local',
				flex : 1,
				id : 'recPurchaseFund',
				padding : '0 5 5 0',
				valueField : 'id',
				displayField : 'fundName',
				autoScroll : true,
				allowBlank : false,
				blankText : 'Please select the Fund',
				msgTarget : 'under',
				editable : false
			}), {
				xtype : 'textfield',
				name : 'name',
				id : 'recDealInsName',
				fieldLabel : 'Name',
				flex : 1,
				allowBlank : false,
				padding : '0 5 5 0'
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'datefield',
				fieldLabel : 'Value Date',
				id : 'recPurchValueDt',
				allowBlank : false,
				flex : 1,
				padding : '0 5 5 0',
				blankText : 'Please select a Value Date',
				msgTarget : 'under',
				format : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				listeners : {
					'change' : {
						fn : function(field, newVal, oldVal) {
							var dfMaturityDate = Ext.ComponentQuery.query('#recPurchMaturityDt')[0];
							if (dfMaturityDate.getValue() != null) {
								var lblMaturityDate = Ext.ComponentQuery.query('#recPurchDuration')[0];
								if (this.getValue() > dfMaturityDate.getValue()) {
									Ext.MessageBox.alert('Error', 'Value Date cannot be greater than Maturity Date.');
									this.setValue(null);
									lblMaturityDate.setText('');
								} else {
									var durationDays = (dfMaturityDate.getValue() - this.getValue()) / 86400000;
									lblMaturityDate.setText('Duration: '.concat(durationDays.toString().concat(' days.')));
								}
								PurchasesDeal.setMatVal();
							}
						}
					}
				}
			}, {
				xtype : 'datefield',
				fieldLabel : 'Matured Date',
				id : 'recPurchMaturityDt',
				allowBlank : false,
				flex : 1,
				padding : '0 5 5 0',
				blankText : 'Please select a Matured Date',
				msgTarget : 'under',
				format : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				minValue : new Date(),
				listeners : {
					'change' : {
						fn : function(field, newVal, oldVal) {
							var dfValueDate = Ext.ComponentQuery.query('#recPurchValueDt')[0];
							if (dfValueDate.getValue() != null) {
								var lblValueDate = Ext.ComponentQuery.query('#recPurchDuration')[0];
								if (dfValueDate.getValue() > this.getValue()) {
									Ext.MessageBox.alert('Error', 'Maturity Date cannot be smaller than Value Date.');
									this.setValue(null);
									lblValueDate.setText('');
								} else {
									var durationDays = (this.getValue() - dfValueDate.getValue()) / 86400000;
									lblValueDate.setText('Duration: '.concat(durationDays.toString().concat(' days.')));
								}
								PurchasesDeal.setMatVal();
							}
						}
					}
				}
			} ]
		}, {
			xtype : 'label',
			id : 'recPurchDuration'
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'combo',
				fieldLabel : 'Delevary Method',
				store : CommenStore.delevaryMethodStore,
				valueField : 'id',
				flex : 1,
				labelWidth : 120,
				name : 'delevarymethode',
				padding : '0 5 0 0',
				displayField : 'code',
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{description}</b><br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {description}', '</tpl>'),
				listeners : {
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
							combo.setValue(null);
						}
					}
				}
			}, {
				xtype : 'combo',
				fieldLabel : 'Dealer',
				store : CommenStore.dealersStore,
				valueField : 'id',
				flex : 1,
				padding : '0 5 0 0',
				name : 'dealerCode',
				displayField : 'code',
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{name}</b><br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {name}', '</tpl>'),
				listeners : {
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
							combo.setValue(null);
						}
					}
				}
			} ]
		}, {
			xtype : 'combo',
			labelWidth : 120,
			fieldLabel : 'Settlement Method',
			store : CommenStore.settlmentStore,
			valueField : 'id',
			anchor : '50%',
			name : 'settlementCode',
			padding : '0 10 0 0',
			displayField : 'code',
			tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{description}</b><br/></div>', '</tpl>'),
			displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {description}', '</tpl>'),
			listeners : {
				change : function(combo, record, index) {
					if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
						combo.setValue(null);
					}
				}
			}
		} ]
	});
};

PurchasesDeal.getInstrumentFieldSet = function() {
	Ext.define('InstrumentPurch', {
		extend : 'Ext.data.Model',
		fields : [ {
			name : 'code',
			type : 'string'
		}, {
			name : 'type',
			type : 'string'
		}, {
			name : 'issuer',
			type : 'string'
		}, {
			name : 'status',
			type : 'string'
		}, {
			name : 'valueDate',
			type : 'date',
			dateFormat : 'c'
		}, {
			name : 'maturityDate',
			type : 'date',
			dateFormat : 'c'
		} ]
	});

	var instrumentStore = new Ext.data.Store({
		model : 'InstrumentPurch',
		storeId : 'recPurchInsStore'
	});

	Ext.Ajax.request({
		url : 'getInstruments.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			var arrayLength = obj.instruemtList.length;
			for (var i = 0; i < arrayLength; i++) {
				instrumentStore.add({
					code : obj.instruemtList[i].instrumentCode,
					type : obj.instruemtList[i].instrumentType.instrumentName,
					issuer : obj.instruemtList[i].issuer.code,
					status : obj.instruemtList[i].status,
					valueDate : obj.instruemtList[i].issueDate,
					maturityDate : obj.instruemtList[i].maturityDate
				});
			}
		}
	});

	Ext.define('InstrumentTypePurch', {
		extend : 'Ext.data.Model',
		fields : [ {
			name : 'id',
			type : 'int'
		}, {
			name : 'instrumentName',
			type : 'string'
		}, {
			name : 'istrumentType',
			type : 'int'
		}, {
			name : 'scripts',
			type : 'int'
		}, {
			name : 'denominals',
			type : 'int'
		}, {
			name : 'listed',
			type : 'int'
		}, {
			name : 'withholdingTax',
			type : 'string'
		}, {
			name : 'interestRate',
			type : 'string'
		}, {
			name : 'interestType',
			type : 'string'
		}, {
			name : 'dayBasis',
			type : 'string'
		}, {
			name : 'couponAvailability',
			type : 'int'
		}, {
			name : 'cuponFrequency',
			type : 'int'
		}, {
			name : 'shortMaturable',
			type : 'int'
		}, {
			name : 'calculationInput',
			type : 'int'
		} ]
	});

	var instrumentTypeStore = new Ext.data.Store({
		model : 'InstrumentTypePurch',
		storeId : 'recPurchInsTypeStore'
	});

	Ext.Ajax.request({
		url : 'getInstrumentTypes.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			instrumentTypeStore.loadRawData(obj.instrumentTypeList);
		}
	});

	return Ext.create('Ext.form.FieldSet', {
		title : 'Instrument',
		defaults : {
			anchor : '100%'
		},
		items : [
				{
					xtype : 'label',
					flex : 1,
					id : 'recDealDealID',
					style : 'font-weight:600;margin-top:15px;',
					text : 'Deal ID: ',
					hidden : true
				},
				{
					xtype : 'fieldcontainer',
					layout : 'hbox',
					items : [
							{
								xtype : 'combo',
								name : 'insType',
								id : 'recDealInstrumentType',
								fieldLabel : 'Instrument Type',
								// store : instrumentTypes,
								queryMode : 'local',
								store : instrumentTypeStore,
								valueField : 'id',
								displayField : 'instrumentName',
								flex : 1,
								padding : '0 5 0 0',
								autoScroll : true,
								allowBlank : false,
								blankText : 'Select the Instrument Type',
								msgTarget : 'under',
								frame : true,
								editable : false,
								listeners : {
									select : {
										fn : function(combo, value) {
											var cbGov = Ext.ComponentQuery.query('#cbIssuerGovernment')[0];
											var cbOth = Ext.ComponentQuery.query('#cbIssuerOther')[0];
											var tfOth = Ext.ComponentQuery.query('#recordDealIssuer')[0];
											var cbPrimaryDealr = Ext.ComponentQuery.query('#recordDealPrimDealer')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('istrumentType') == 1) {
												cbGov.setValue(true);
												cbOth.setValue(false);
												// tfOth.clearValue();
												tfOth.disable();
												// cbPrimaryDealr.clearValue();
												cbPrimaryDealr.enable();
												cbPrimaryDealr.allowBlank = false;
												tfOth.allowBlank = true;
											} else {
												cbGov.setValue(false);
												cbOth.setValue(true);
												// tfOth.clearValue();
												tfOth.enable();
												// cbPrimaryDealr.clearValue();
												cbPrimaryDealr.disable();
												tfOth.allowBlank = false;
												cbPrimaryDealr.allowBlank = true;
											}
											var fcpn = Ext.ComponentQuery.query('#recDealCouponField')[0];
											var tfcpnRt = Ext.ComponentQuery.query('#recDealCouponFreq')[0];
											var fscpnRt = Ext.ComponentMgr.get('recDealCoupnData');
											var cbCpnDtFix = Ext.ComponentQuery.query('#recDealCpnFixcb')[0];
											var cbCpnDtVar = Ext.ComponentQuery.query('#recDealCpnVarycb')[0];
											var cbCpnDtNo = Ext.ComponentQuery.query('#recDealCpnNo')[0];
											var dpCpnDts = Ext.ComponentQuery.query('#recDealCpnDts')[0];
											var pCpnDtStore = Ext.ComponentQuery.query('#recordDealDtStore')[0];
											var nfCouponRate = Ext.ComponentQuery.query('#recDealCouponRate')[0];
											if ((instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('couponAvailability') == 1)
													|| (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('couponAvailability') == 2)) {
												fcpn.enable();
												fscpnRt.show();
												fscpnRt.enable();
												// nfCouponRate.reset(),
												nfCouponRate.enable();
												if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('couponAvailability') === 1) {
													cbCpnDtFix.setValue(true);
													cbCpnDtVar.setValue(false);
													cbCpnDtNo.setValue(false);
													tfcpnRt.setValue(instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('cuponFrequency'));
													fscpnRt.show();
													fscpnRt.enable();
													tfcpnRt.show();
													dpCpnDts.hide();
													dpCpnDts.disable();
													pCpnDtStore.hide();
												} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('couponAvailability') === 2) {
													fscpnRt.show();
													fscpnRt.enable();
													cbCpnDtFix.setValue(false);
													cbCpnDtVar.setValue(true);
													cbCpnDtNo.setValue(false);
													tfcpnRt.hide();
													dpCpnDts.show();
													dpCpnDts.enable();
													pCpnDtStore.show();
												} else {
													fscpnRt.hide();
													fscpnRt.disable();
													cbCpnDtFix.setValue(false);
													cbCpnDtVar.setValue(false);
													cbCpnDtNo.setValue(true);
													tfcpnRt.hide();
													dpCpnDts.hide();
													dpCpnDts.disable();
													pCpnDtStore.hide();
												}
												// tfcpnRt.disable();
											} else {
												fscpnRt.hide();
												fscpnRt.disable();
												fcpn.disable();
												cbCpnDtFix.setValue(false);
												cbCpnDtVar.setValue(false);
												cbCpnDtNo.setValue(true);
												tfcpnRt.hide();
												dpCpnDts.hide();
												dpCpnDts.disable();
												pCpnDtStore.hide();
												// nfCouponRate.reset();
												nfCouponRate.disable();
											}

											var cbIntTypeFix = Ext.ComponentQuery.query('#recDealIntTypeFix')[0];
											var cbIntTypeFloat = Ext.ComponentQuery.query('#recDealIntTypeFloat')[0];
											// var fsFloatField1 =
											// Ext.ComponentQuery.query('#recPurchFloatField1')[0];
											var rstPrd = Ext.ComponentQuery.query('#RecPurchResetPeriod')[0];
											var rstDt = Ext.ComponentQuery.query('#recPurchFloatResetDt')[0];
											var rstBench = Ext.ComponentQuery.query('#recPurchBench')[0];
											var rstFlr = Ext.ComponentQuery.query('#recPurchFloor')[0];
											var rstClng = Ext.ComponentQuery.query('#recPurchCeiling')[0];
											// var fsFloatField2 =
											// Ext.ComponentQuery.query('#recPurchFloatField2')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('interestRate') == 'Fixed') {
												cbIntTypeFix.setValue(true);
												cbIntTypeFloat.setValue(false);
												// fsFloatField1.disable();
												rstPrd.disable();
												rstDt.disable();
												rstBench.disable();
												rstFlr.disable();
												rstClng.disable();
												// fsFloatField1.hide();
												rstPrd.hide();
												rstDt.hide();
												rstBench.hide();
												rstFlr.hide();
												rstClng.hide();
												// fsFloatField2.disable();
												// fsFloatField2.hide();
											} else {
												cbIntTypeFix.setValue(false);
												cbIntTypeFloat.setValue(true);
												// fsFloatField1.enable();
												rstPrd.enable();
												rstDt.enable();
												rstBench.enable();
												rstFlr.enable();
												rstClng.enable();
												// fsFloatField1.show();
												rstPrd.show();
												rstDt.show();
												rstBench.show();
												rstFlr.show();
												rstClng.show();
												// fsFloatField2.enable();
												// fsFloatField2.show();
											}

											var rbNoWhT = Ext.ComponentQuery.query('#recDealWhTNo')[0];
											var rbUpWhT = Ext.ComponentQuery.query('#recDealWhTUp')[0];
											var rbMatWhT = Ext.ComponentQuery.query('#recDealWhTMat')[0];

											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('withholdingTax') === 'No') {
												rbNoWhT.setValue(true);
												rbUpWhT.setValue(false);
												rbMatWhT.setValue(false);
												rbNoWhT.disable();
												rbUpWhT.disable();
												rbMatWhT.disable();
											} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('withholdingTax') === 'Upfront') {
												rbNoWhT.setValue(false);
												rbUpWhT.setValue(true);
												rbMatWhT.setValue(false);
												rbNoWhT.disable();
												rbUpWhT.disable();
												rbMatWhT.disable();
											} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('withholdingTax') === 'Maturity') {
												rbNoWhT.setValue(false);
												rbUpWhT.setValue(false);
												rbMatWhT.setValue(true);
												rbNoWhT.disable();
												rbUpWhT.disable();
												rbMatWhT.disable();
											} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('withholdingTax') === 'Vary') {
												rbNoWhT.setValue(true);
												rbUpWhT.setValue(false);
												rbMatWhT.setValue(false);
												rbNoWhT.enable();
												rbUpWhT.enable();
												rbMatWhT.enable();
											}

											var comDayBas = Ext.ComponentQuery.query('#recDealDayBasis')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('dayBasis') == 'Vary') {
												comDayBas.enable();
											} else {
												comDayBas.setValue(instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('dayBasis'));
												comDayBas.disable();
											}

											var cbScripts = Ext.ComponentQuery.query('#recDealScripts')[0];
											var cbDenominals = Ext.ComponentQuery.query('#recDealdenominals')[0];
											var tfMinDenomi = Ext.ComponentQuery.query('#recDealDenominamMinVal')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('scripts') == 1) {
												cbScripts.setValue(true);
												if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('denominals') == 1) {
													cbDenominals.setValue(true);
													tfMinDenomi.enable();
												} else {
													cbDenominals.setValue(false);
													tfMinDenomi.disable();
												}
											} else {
												cbScripts.setValue(false);
												cbDenominals.setValue(false);
												// tfMinDenomi.reset();
												tfMinDenomi.disable();
											}

											var rbStraight = Ext.ComponentQuery.query('#recDealIntCalStr')[0];
											var rbDiscounted = Ext.ComponentQuery.query('#recDealIntCalDis')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('interestType') === 'Discounted') {
												rbStraight.setValue(false);
												rbDiscounted.setValue(true);
											} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('interestType') === 'Straight') {
												rbStraight.setValue(true);
												rbDiscounted.setValue(false);
											} else {
												rbStraight.setValue(false);
												rbDiscounted.setValue(false);
												Ext.Msg.alert('Error', 'Invalid Interest Calculation Method Found!');
											}

											var lblListed = Ext.ComponentQuery.query('#recDealListedLabel')[0];
											var comboInsCode = Ext.ComponentQuery.query('#recDealInstrumentCodeCombo')[0];
											var tfInsCode = Ext.ComponentQuery.query('#recDealInstrumentCodeText')[0];

											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('listed') === 1) {
												lblListed.setText('Listed Instrument');
												instrumentStore.clearFilter(true);
												// instrumentStore.filter('type',
												// instrumentTypeStore.getAt(instrumentTypeStore.find('id',
												// combo.getValue())).get('instrumentName'));
												instrumentStore.filterBy(function(rec, id) {
													if (rec.get('type') === instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('instrumentName'))
														return true;
													else
														return false;
												});
												comboInsCode.bindStore(instrumentStore);
												tfInsCode.disable();
												tfInsCode.hide();
												// comboInsCode.clearValue();
												comboInsCode.show();
												comboInsCode.enable();
											} else {
												lblListed.setText('Non-Listed Instrument');
												comboInsCode.hide();
												comboInsCode.disable();
												tfInsCode.show();
												tfInsCode.enable();
												var tfInsName = Ext.ComponentQuery.query('#recDealInsName')[0];
												// tfInsName.reset();
												tfInsName.enable();
												var dfValDate = Ext.ComponentQuery.query('#recPurchValueDt')[0];
												// dfValDate.reset();
												dfValDate.enable();
												var dfMatDate = Ext.ComponentQuery.query('#recPurchMaturityDt')[0];
												// dfMatDate.reset();
												dfMatDate.enable();
											}

											var maturityValuetf = Ext.ComponentQuery.query('#recDealMaturityValue')[0];
											var dealValuetf = Ext.ComponentQuery.query('#recPurchDealVal')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('calculationInput') == 2) {
												dealValuetf.disable();
												maturityValuetf.enable();
											} else {
												maturityValuetf.disable();
												dealValuetf.enable();
											}

											Ext.Ajax.request({
												url : 'getFundsForDealInsType.action',
												params : {
													querry : 'PURCH',
													querryTwo : combo.getValue()
												},
												success : function(response) {
													var obj = Ext.decode(response.responseText);
													Ext.data.StoreManager.lookup('dealFundsStore').loadRawData(obj.fundList);
												}
											});
											Ext.ComponentQuery.query('#recDealMaturityValue')[0].reset();
										}
									},
								}
							}, {
								xtype : 'label',
								flex : 1,
								id : 'recDealListedLabel',
								style : 'font-weight:600;'
							} ]
				}, Ext.create('Ext.form.ComboBox', {
					name : 'listedInsCode',
					id : 'recDealInstrumentCodeCombo',
					fieldLabel : 'Instrument Code',
					store : instrumentStore,
					queryMode : 'local',
					valueField : 'code',
					displayField : 'code',
					anchor : '50%',
					padding : '0 5 0 0',
					style : 'margin-top:10px;',
					autoScroll : true,
					allowBlank : false,
					blankText : 'Select the Instrument Code',
					msgTarget : 'under',
					editable : false,
					hidden : true,
					tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{issuer}</b><br/></div>', '</tpl>'),
					displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code}', '</tpl>'),
					listeners : {
						select : {
							fn : function(combo, value) {
								var comboInsType = Ext.ComponentQuery.query('#recDealInstrumentType')[0];

								console.log(instrumentTypeStore.getAt(instrumentTypeStore.find('id', comboInsType.getValue())).get('listed'));
								if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', comboInsType.getValue())).get('listed') === 1) {
									var tfInsName = Ext.ComponentQuery.query('#recDealInsName')[0];
									tfInsName.setValue(instrumentStore.getAt(instrumentStore.find('code', combo.getValue())).get('name'));
									// tfInsName.disable();

									var dfValDate = Ext.ComponentQuery.query('#recPurchValueDt')[0];
									dfValDate.format = 'd/m/Y';
									dfValDate.setValue(instrumentStore.getAt(instrumentStore.find('code', combo.getValue())).get('valueDate'));
									// dfValDate.disable();

									var dfMatDate = Ext.ComponentQuery.query('#recPurchMaturityDt')[0];
									dfMatDate.format = 'd/m/Y';
									dfMatDate.setValue(instrumentStore.getAt(instrumentStore.find('code', combo.getValue())).get('maturityDate'));
									// dfMatDate.disable();
									console.log(instrumentStore.getAt(instrumentStore.find('code', combo.getValue())).get('maturityDate'));
								}

								if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', comboInsType.getValue())).get('couponAvailability') === 2) {
									Ext.Ajax.request({
										url : 'queryCouponDatesByCode.action',
										params : {
											queryOne : combo.getValue(),
										},
										success : function(response) {
											var obj = Ext.decode(response.responseText);
											if (obj.couponDates != null && obj.couponDates != '') {
												var cpnDtsArray = obj.couponDates.split(";");
												var cpnDtStoreLocal = Ext.data.StoreManager.lookup('couponDatesStore');
												cpnDtStoreLocal.removeAll();
												var i = 0;
												for (i = 0; i < cpnDtsArray.length - 1; i++) {
													cpnDtStoreLocal.add({
														couponDate : cpnDtsArray[i],
													});
												}
												cpnDtStoreLocal.sort('couponDate', 'ASC');
											}
										},
										failure : function(response) {
											Ext.Msg.alert('Error!', 'Failed to load Coupon Dates : ' + response);
										},
									});
								}
							}
						}
					}
				}), {
					xtype : 'textfield',
					name : 'insCode',
					fieldLabel : 'Instrument Code',
					id : 'recDealInstrumentCodeText',
					allowBlank : false,
					anchor : '50%',
					padding : '0 5 0 0',
					style : 'margin-top:10px;',
					blankText : 'Instrument Code is required',
					msgTarget : 'under',
					listeners : {
						'blur' : function(field) {
							find = instrumentStore.find('code', field.getValue());
							if (find != -1) {
								Ext.Msg.alert(field.getValue() + " is already used!");
								this.reset();
							}
						}
					}
				}, {
					xtype : 'label',
					flex : 1,
					id : 'recDealStatusLbl',
					style : 'font-weight:600;margin-top:15px;',
					text : 'Deal ID: ',
					hidden : true
				} ]
	});
};

PurchasesDeal.getDealFieldSet = function() {
	return Ext.create('Ext.form.FieldSet', {
		title : 'Deal',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				id : 'recPurchDealVal',
				minValue : 0,
				name : 'dealValue',
				fieldLabel : 'Deal Value',
				allowBlank : false,
				blankText : 'Please enter the Deal Amount',
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value.replace(/,/g, ''), '0,000.0000'));
						PurchasesDeal.setMatVal();
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			}, {
				xtype : 'textfield',
				minValue : 0,
				name : 'placementFee',
				id : 'recDealPlacementFee',
				fieldLabel : 'Placement Fee',
				allowBlank : false,
				blankText : 'Please enter the Placement Fee',
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value.replace(/,/g, ''), '0,000.0000'));
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'checkboxfield',
				boxLabel : 'Secondary Market',
				id : 'recPurchSecMarket',
				name : 'secMarket',
				inputValue : 1,
				flex : 1,
				padding : '0 5 5 0',
				listeners : {
					change : {
						fn : function(object, Value) {
							if (Value) {
								var ssname = Ext.ComponentQuery.query('#secSellerName')[0];
								ssname.enable();
								var ssbank = Ext.ComponentQuery.query('#secSellerBank')[0];
								ssbank.enable();
							} else {
								var ssname = Ext.ComponentQuery.query('#secSellerName')[0];
								ssname.disable();
								var ssbank = Ext.ComponentQuery.query('#secSellerBank')[0];
								ssbank.disable();
							}
						}
					}
				}
			}, {
				xtype : 'textfield',
				minValue : 0,
				id : 'recDealMaturityValue',
				fieldLabel : 'Maturity Value',
				allowBlank : false,
				name : 'maturityValue',
				disabled : true,
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value.replace(/,/g, ''), '0,000.0000'));
						PurchasesDeal.setMatVal();
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			style : 'margin-top:10px;',
			items : [ {
				xtype : 'textfield',
				name : 'secSellerName',
				id : 'secSellerName',
				fieldLabel : "Seller's Name",
				disabled : true,
				flex : 1,
				padding : '0 5 5 0'
			}, {
				xtype : 'textfield',
				name : 'secSellerBankNo',
				id : 'secSellerBank',
				fieldLabel : "Bank No",
				disabled : true,
				flex : 1,
				padding : '0 5 5 0'
			} ]
		} ]
	});
};

PurchasesDeal.getInstrumentDetailFieldSet = function() {

	var cpnDtStore = Ext.create('Ext.data.Store', {
		storeId : 'couponDatesStore',
		fields : [ {
			name : 'couponDate',
			type : 'date',
			dateFormat : 'd/m/Y'
		} ]
	});

	var primaryDealerStore = Ext.create('Ext.data.Store', {
		storeId : 'primaryDealersStore',
		fields : [ {
			name : 'id',
			type : 'int'
		}, {
			name : 'name',
			type : 'string'
		}, {
			name : 'code',
			type : 'string'
		}, {
			name : 'creditRating',
			type : 'string'
		} ]
	});

	var issuersStore = Ext.create('Ext.data.Store', {
		storeId : 'issuersStore',
		fields : [ {
			name : 'id',
			type : 'int'
		}, {
			name : 'name',
			type : 'string'
		}, {
			name : 'code',
			type : 'string'
		}, {
			name : 'creditRating',
			type : 'string'
		} ]
	});

	Ext.Ajax.request({
		url : 'getPrimaryDealers.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			primaryDealerStore.loadRawData(obj.issuers);
		}
	});

	Ext.Ajax.request({
		url : 'getIssuers.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			issuersStore.loadRawData(obj.issuerList);
		}
	});

	var valDate = Ext.ComponentQuery.query('#recPurchValueDt')[0];
	var matDate = Ext.ComponentQuery.query('#recPurchMaturityDt')[0];

	return Ext.create('Ext.form.FieldSet', {
		title : 'Instrument Details',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'fieldcontainer',
				labelWidth : 115,
				fieldLabel : 'Instrument Type',
				defaultType : 'radiofield',
				flex : 1,
				padding : '0 5px 0 0',
				layout : 'vbox',
				items : [ {
					boxLabel : 'Government',
					padding : '0 40 0 0',
					id : 'cbIssuerGovernment',
					disabled : true
				}, {
					boxLabel : 'Other',
					id : 'cbIssuerOther',
					disabled : true
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'vbox',
				flex : 1,
				padding : '0 5px 0 0',
				items : [ Ext.create('Ext.form.ComboBox', {
					fieldLabel : 'Primary Dealer',
					store : primaryDealerStore,
					valueField : 'id',
					name : 'primaryDealer',
					queryMode : 'local',
					id : 'recordDealPrimDealer',
					autoScroll : true,
					editable : false,
					disabled : true,
					// flex : 1,
					padding : '0 10px 0 0',
					tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item"><b>{code}</b>-{name}-<b>{creditRating}</b><br/></div>', '</tpl>'),
					displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{name}', '</tpl>')

				}), Ext.create('Ext.form.ComboBox', {
					fieldLabel : 'Issuer',
					store : issuersStore,
					valueField : 'id',
					name : 'issuer',
					// displayField : 'name',
					queryMode : 'local',
					id : 'recordDealIssuer',
					autoScroll : true,
					editable : false,
					disabled : true,
					padding : '0 10px 0 0',
					tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item"><b>{code}</b>-{name}-<b>{creditRating}</b><br/></div>', '</tpl>'),
					displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{name}', '</tpl>')
				}) ]
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			style : 'margin-top:10px;',
			items : [ {
				xtype : 'fieldcontainer',
				labelWidth : 115,
				fieldLabel : 'Coupon Availability',
				defaultType : 'radiofield',
				id : 'recDealCouponField',
				flex : 1,
				layout : 'hbox',
				items : [ {
					boxLabel : 'No',
					id : 'recDealCpnNo',
					disabled : true
				}, {
					boxLabel : 'Fixed',
					id : 'recDealCpnFixcb',
					disabled : true
				}, {
					boxLabel : 'Vary',
					id : 'recDealCpnVarycb',
					disabled : true
				} ]
			}, {
				xtype : 'numberfield',
				minValue : 0,
				maxValue : 100,
				decimalPrecision : 4,
				flex : 1,
				padding : '0 5px 0 0',
				name : 'couponRate',
				id : 'recDealCouponRate',
				fieldLabel : 'Coupon Rate',
				// *allowBlank : false,
				blankText : 'Coupon Rate is required',
				msgTarget : 'under',
				disabled : true
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			hidden : true,
			id : 'recDealCoupnData',
			style : 'margin-top:10px;',
			items : [ {
				xtype : 'numberfield',
				minValue : 0,
				labelWidth : 115,
				maxValue : 100,
				decimalPrecision : 0,
				// padding : '0 0 5 0',
				name : 'couponFreq',
				anchor : '60%',
				id : 'recDealCouponFreq',
				fieldLabel : 'Coupon Frequency',
				disabled : true,
				hidden : true,
				allowBlank : false
			}, {
				xtype : 'datefield',
				fieldLabel : 'Coupon Dates',
				id : 'recDealCpnDts',
				minValue : new Date(),
				flex : 1,
				padding : '0 5px 0 0',
				labelWidth : 115,
				// allowBlank : false,
				blankText : 'Please select a Start Date',
				msgTarget : 'under',
				format : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				hidden : true,
				disabled : true,
				listeners : {
					change : {
						fn : function(object, newVal, oldVal) {
							var recordIndex = cpnDtStore.findBy(function(record, id) {
								if (newVal != null && (record.get('couponDate').getTime() == newVal.getTime())) {
									return true;
								}
								return false;
							});
							if (recordIndex != -1) {
								Ext.MessageBox.alert('Error!', 'Duplicate Entry!!!');
							} else if (newVal == null) {
								Ext.MessageBox.alert('Error', 'Please Select a Start Date');
								console.log("Null value for date field");
							} else if (valDate.getValue() == null || matDate.getValue() == null) {
								Ext.MessageBox.alert('Error!', 'Deal Period not specified!');
							} else if (newVal < valDate.getValue() || newVal > matDate.getValue()) {
								Ext.MessageBox.alert('Error!', 'Coupon Date should be within the Deal Period!');
							} else {
								cpnDtStore.add({
									couponDate : Ext.util.Format.date(newVal, 'd/m/Y')
								});
							}
						}
					}
				}
			}, Ext.create('Ext.grid.Panel', {
				width : 100,
				height : 130,
				flex : 1,
				store : cpnDtStore,
				id : 'recordDealDtStore',
				autoScroll : true,
				columns : [ {
					xtype : 'datecolumn',
					header : 'Coupon Dates',
					dataIndex : 'couponDate',
					format : 'd/m/Y',
					flex : 1
				} ],
				hidden : true,
				bbar : [ {
					xtype : 'button',
					text : 'Remove',
					handler : function() {
						var pCpnDtStore = Ext.ComponentQuery.query('#recordDealDtStore')[0];
						var sm = pCpnDtStore.getSelectionModel();
						var sel = sm.getSelection();
						var i = 0;
						var currentDate = new Date();
						for (i = 0; i < sel.length; i++) {
							if (sel[i].get("couponDate") > currentDate) {
								pCpnDtStore.store.remove(sel[i]);
							} else {
								Ext.Msg.alert('Error', 'Cannot Remove Previous Dates!');
							}
						}
					}
				} ]
			}) ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'fieldcontainer',
				fieldLabel : 'Interest Type',
				defaultType : 'radiofield',
				layout : 'hbox',
				flex : 1,
				labelWidth : 115,
				style : 'margin-top:10px;',
				items : [ {
					boxLabel : 'Fixed',
					id : 'recDealIntTypeFix',
					disabled : true
				}, {
					boxLabel : 'Floating',
					id : 'recDealIntTypeFloat',
					disabled : true
				} ]
			}, {
				xtype : 'numberfield',
				minValue : 0,
				maxValue : 100,
				decimalPrecision : 4,
				padding : '0 5px 0 0',
				name : 'interestRate',
				fieldLabel : 'Rate',
				style : 'margin-top:10px;',
				flex : 1,
				id : 'recPurchInterestRate',
				allowBlank : false,
				blankText : 'Rate is required',
				msgTarget : 'under',
				listeners : {
					blur : function() {
						PurchasesDeal.setMatVal();
					}
				}
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			id : 'recPurchFloatField1',
			// hidden : true,

			items : [ {
				xtype : 'numberfield',
				id : 'RecPurchResetPeriod',
				labelWidth : 115,
				fieldLabel : 'Reset Period',
				name : 'rateResetPeriod',
				flex : 1,
				hidden : true,
				minValue : 1,
				decimalPrecision : 0,
				allowBlank : false,
				padding : '0 79px 0 0'
			}, {
				xtype : 'datefield',
				fieldLabel : 'Reset Date',
				id : 'recPurchFloatResetDt',
				flex : 1,
				hidden : true,
				padding : '0 5px 0 0',
				format : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				minValue : new Date(),
				allowBlank : false
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			id : 'recPurchFloatField2',
			// hidden : true,
			style : 'margin-top:10px;',
			items : [ {
				xtype : 'combo',
				labelWidth : 115,
				fieldLabel : 'Bench Mark',
				store : benchMarkStore,
				displayField : 'name',
				valueField : 'name',
				name : 'benchMark',
				id : 'recPurchBench',
				flex : 1.5,
				hidden : true,
				padding : '0 5px 0 0',
				typeAhead : true,
				typeAheadDelay : 100,
				listConfig : {
					loadingText : 'Searching...',
					emptyText : 'No matching posts found.'
				}
			}, {
				xtype : 'numberfield',
				id : 'recPurchFloor',
				minValue : 0,
				maxValue : 100,
				decimalPrecision : 4,
				name : 'floorValue',
				labelWidth : 50,
				fieldLabel : 'Floor',
				allowBlank : false,
				flex : 1,
				hidden : true,
				padding : '0 5px 0 0'
			}, {
				xtype : 'numberfield',
				id : 'recPurchCeiling',
				minValue : 0,
				maxValue : 100,
				decimalPrecision : 4,
				name : 'ceilingValue',
				allowBlank : false,
				fieldLabel : 'Ceiling',
				labelWidth : 50,
				flex : 1,
				hidden : true,
				padding : '0 5px 0 0'
			} ]
		}, {
			// xtype : 'fieldcontainer',
			xtype : 'radiogroup',
			fieldLabel : 'Withholding Tax',
			// defaultType : 'radiofield',
			labelWidth : 115,
			layout : 'hbox',
			padding : '0 5 0 0',
			style : 'margin-top:10px;',
			flex : 1,
			items : [ {
				boxLabel : 'No',
				id : 'recDealWhTNo',
				name : 'withholdingTax',
				inputValue : 'No',
				padding : '0 3 0 0',
				flex : 1,
				checked : true,
				disabled : true,
				listeners : {
					change : function(field, newValue, oldValue) {
						if (newValue) {
							var whtrate = Ext.ComponentQuery.query('#withholdingTaxRate')[0];
							whtrate.setValue("");
							whtrate.disable();
							whtrate.allowBlank = true;
							PurchasesDeal.setMatVal();
						} else {
							var whtrate = Ext.ComponentQuery.query('#withholdingTaxRate')[0];
							whtrate.setValue("");
							whtrate.enable();
							whtrate.allowBlank = false;
						}
					}
				}
			}, {
				boxLabel : 'Upfront',
				id : 'recDealWhTUp',
				name : 'withholdingTax',
				padding : '0 3 0 0',
				flex : 1,
				inputValue : 'Upfront',
				disabled : true,
				listeners : {
					change : function(field, newValue, oldValue) {
						if (newValue) {
							console.log('U:::::' + newValue + ' : ' + oldValue);
							PurchasesDeal.setMatVal('Upfront');
						}
					}
				}
			}, {
				boxLabel : 'Maturity',
				id : 'recDealWhTMat',
				name : 'withholdingTax',
				padding : '0 3 0 0',
				flex : 1,
				inputValue : 'Maturity',
				disabled : true,
				listeners : {
					change : function(field, newValue, oldValue) {
						if (newValue) {
							console.log('M:::::' + newValue + ' : ' + oldValue);
							PurchasesDeal.setMatVal('Maturity');
						}
					}
				}
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				style : 'margin-top:10px;',
				fieldLabel : 'Withholding Tax Rate',
				flex : 1,
				disabled : true,
				fieldStyle : "text-align:right;",
				maskRe : /[\d%.]/,
				id : 'withholdingTaxRate',
				name : 'withholdingTaxRate',
				padding : '0 10 0 0',
				listeners : {
					blur : function() {
						if (this.getRawValue() <= 100) {
							this.setRawValue(Ext.util.Format.number(this.value.replace(/,/g, ''), '0.0000%'));
						} else {
							this.setRawValue('0.0000%');
						}
						PurchasesDeal.setMatVal();
					},
					render : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0.0000%'));
					}
				}
			}, new Ext.form.ComboBox({
				store : new Ext.data.Store({
					fields : [ 'dayBasis' ],
					data : [ {
						"dayBasis" : "Act / Act"
					}, {
						"dayBasis" : "Act / 360"
					}, {
						"dayBasis" : "Act / 364"
					}, {
						"dayBasis" : "Act / 365"
					}, {
						"dayBasis" : "30 / 360"
					}, {
						"dayBasis" : "30 / 365"
					} ]
				}),
				displayField : 'dayBasis',
				valueField : 'dayBasis',
				name : 'dayBasis',
				mode : 'local',
				value : 'Act / 365',
				fieldLabel : 'Day Basis',
				forceSelection : true,
				disabled : true,
				labelWidth : 30,
				name : 'dayBasis',
				style : 'margin-top:10px;',
				id : 'recDealDayBasis',
				flex : 1,
				padding : '0 5 0 0',
				listeners : {
					blur : function() {
						PurchasesDeal.setMatVal();
					}
				}
			}) ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'checkboxfield',
				boxLabel : 'Scripts',
				name : 'scripts',
				inputValue : 1,
				id : 'recDealScripts',
				disabled : true,
				padding : '0 10 0 0',
				style : 'margin-top:10px;',
				flex : 1
			}, {
				xtype : 'checkboxfield',
				boxLabel : 'Denominals',
				name : 'denominals',
				inputValue : 1,
				id : 'recDealdenominals',
				disabled : true,
				padding : '0 10 0 0',
				style : 'margin-top:10px;',
				flex : 1
			}, {
				xtype : 'numberfield',
				minValue : 0,
				decimalPrecision : 4,
				padding : '0 5 0 0',
				style : 'margin-top:10px;',
				flex : 2,
				name : 'denominalMinVal',
				id : 'recDealDenominamMinVal',
				fieldLabel : 'Minimum Value',
				disabled : true,
				allowBlank : false
			} ]
		}, {
			xtype : 'fieldcontainer',
			fieldLabel : 'Interest Calculation Method',
			defaultType : 'radiofield',
			labelWidth : 115,
			padding : '0 0 5 0',
			style : 'margin-top:10px;',
			layout : 'hbox',
			items : [ {
				boxLabel : 'Straight',
				name : 'intCalMethod',
				id : 'recDealIntCalStr',
				inputValue : 'straight',
				padding : '0 40 0 0',
				disabled : true
			}, {
				boxLabel : 'Discounted',
				name : 'intCalMethod',
				id : 'recDealIntCalDis',
				inputValue : 'discounted',
				disabled : true
			} ]
		} ]
	});
};

PurchasesDeal.getCenterPanel = function() {

	var basic = PurchasesDeal.getBasicFieldSet();
	var instrument = PurchasesDeal.getInstrumentFieldSet();
	var deal = PurchasesDeal.getDealFieldSet();
	var insDtl = PurchasesDeal.getInstrumentDetailFieldSet();
	formPanel = Ext.create('Ext.form.FormPanel', {
		title : 'New Purchase Record',
		id : 'recPurchaseMainPanel',
		bodyPadding : '20',
		autoScroll : true,
		frame : true,
		defaults : {
			anchor : '72%'
		},
		fieldDefaults : {
			labelAlign : 'left'
		},
		items : [ {
			xtype : 'fieldcontainer',
			items : [ {
				xtype : 'triggerfield',
				id : 'recPurchTrigField',
				triggerCls : 'x-form-search-trigger',
				anchor : '28%',
				style : 'float:right;',
				emptyText : 'Search for a Purchase',
				listeners : {
					specialkey : function(field, e) {
						if (e.getKey() == e.ENTER) {
							Ext.Msg.alert('HI', 'Press Enter');
						}
					}
				},
				onTriggerClick : function() {
					var searchResult = Ext.create('Ext.data.Store', {
						fields : [ 'id', 'dealCode', 'issuer', 'name', 'status' ]
					});
					var TrigField = Ext.ComponentQuery.query('#recPurchTrigField')[0];

					Ext.Ajax.request({
						url : 'getPurchases.action',
						params : {
							query : TrigField.getValue()
						},
						success : function(response) {
							var obj = Ext.decode(response.responseText);
							var arrayLength = obj.deals.length;
							for (var i = 0; i < arrayLength; i++) {
								searchResult.add({
									id : obj.deals[i].id,
									dealCode : obj.deals[i].dealCode,
									issuer : obj.deals[i].issuer.code,
									name : obj.deals[i].name,
									status : obj.deals[i].status
								});
							}
						}
					});

					var resultWindow = Ext.create('Ext.grid.Panel', {
						defaults : {
							anchor : '100%'
						},
						store : searchResult,
						columns : [ {
							text : 'Deal Code',
							dataIndex : 'dealCode',
							flex : 1
						}, {
							text : 'Issuer',
							dataIndex : 'issuer',
							flex : 1
						}, {
							text : 'Name',
							dataIndex : 'name',
							flex : 2
						}, {
							text : 'Status',
							dataIndex : 'status',
							flex : 1
						} ],

						listeners : {
							itemdblclick : function(view, rec, item, index, eventObj) {
								var findId = rec.get('id');
								var formPanl = Ext.ComponentQuery.query('#recPurchaseMainPanel')[0];

								// var confirmFields = '';
								var updateBtn = Ext.getCmp('recPurchUpdateBtn');
								var saveBtn = Ext.getCmp('recPurchSaveBtn');
								// var confirmBtn =
								// Ext.getCmp('recPurchConfirmBtn');
								Ext.ComponentQuery.query('#recDealInstrumentType')[0].disable();
								Ext.ComponentQuery.query('#recDealInstrumentCodeCombo')[0].disable();
								Ext.ComponentQuery.query('#recDealInstrumentCodeText')[0].disable();
								Ext.ComponentQuery.query('#recPurchaseFund')[0].disable();

								formPanl.getForm().reset();
								Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
								formPanl.setLoading('Receiving data...', formPanl.getForm());
								Ext.Ajax.request({
									url : 'getPurchase.action',
									params : {
										id : findId
									},
									success : function(response) {
										var obj = Ext.decode(response.responseText);
										var tempInsType = obj.data.insType;
										var tempInsTypeCmb = Ext.ComponentQuery.query('#recDealInstrumentType')[0];
										var record = tempInsTypeCmb.store.getById(tempInsType);
										tempInsTypeCmb.select(tempInsType);
										tempInsTypeCmb.fireEvent('select', tempInsTypeCmb, record);

										var valDtLocal = Ext.ComponentQuery.query('#recPurchValueDt')[0];
										valDtLocal.format = 'd/m/Y';
										valDtLocal.setValue(obj.data.valueDate);

										var matDtLocal = Ext.ComponentQuery.query('#recPurchMaturityDt')[0];
										matDtLocal.format = 'd/m/Y';
										matDtLocal.setValue(obj.data.maturityDate);

										if (obj.data.resetDate != null) {
											var matDtLocal = Ext.ComponentQuery.query('#recPurchFloatResetDt')[0];
											matDtLocal.format = 'd/m/Y';
											matDtLocal.setValue(obj.data.resetDate);
										}

										var localInsStore = Ext.data.StoreManager.lookup('recPurchInsStore');
										localInsStore.clearFilter(true);

										var lblDealID = Ext.ComponentQuery.query('#recDealDealID')[0];
										lblDealID.show();
										lblDealID.setText('Deal ID: ' + obj.data.dealCode);

										var lblDealStatus = Ext.ComponentQuery.query('#recDealStatusLbl')[0];
										lblDealStatus.show();
										lblDealStatus.setText('Status: ' + obj.data.status);

										if (obj.data.params != null) {
											Ext.getCmp('recPurchParamLbl').setText(obj.data.params);
										}

										if (!(!obj.data.couponDates || 0 === obj.data.couponDates.length)) {
											var cpnDtsArray = obj.data.couponDates.split(";");
											var cpnDtStoreLocal = Ext.data.StoreManager.lookup('couponDatesStore');
											cpnDtStoreLocal.removeAll();
											var i = 0;
											for (i = 0; i < cpnDtsArray.length - 1; i++) {
												cpnDtStoreLocal.add({
													couponDate : cpnDtsArray[i]
												});
											}
										}

										Ext.Ajax.request({
											url : 'getFundsForDealInsType.action',
											params : {
												querry : 'PURCH',
												querryTwo : tempInsType
											},
											success : function(response) {
												var obj = Ext.decode(response.responseText);
												Ext.data.StoreManager.lookup('dealFundsStore').loadRawData(obj.fundList);

												formPanl.getForm().load({
													url : 'getPurchase.action',
													params : {
														id : findId
													},
													success : function(response) {
														formPanl.setLoading(false);
													},
													failure : function(form, action) {
														formPanl.setLoading(false);
														console.log(arguments);
														Ext.Msg.alert("An Error occured. Load failed");
													}
												});
											},
											failure : function(form, action) {
												Ext.Msg.alert('Error!', 'Failed to load Fund List!');
												formPanl.setLoading(false);
											}
										});
									},
									failure : function(form, action) {
										formPanl.setLoading(false);
									}
								});
								if (rec.get('status') === 'Initial' && 'user' === 'user') { // If
									// only
									// viewing
									// permissions
									saveBtn.setVisible(false);
									saveBtn.disable();
									// confirmBtn.setVisible(false);
									updateBtn.setVisible(true);
									updateBtn.enable();
								} else {
									formPanl.getForm().getFields().each(function(field) {
										field.setDisabled(true);
									});
									saveBtn.disable();
									saveBtn.setVisible(true);
									updateBtn.setVisible(false);
									updateBtn.disable();

								}
								this.up('.window').close();
								Ext.ComponentQuery.query('#recPurchTrigField')[0].enable();
								// formPanl.setLoading(false);

							}

						}
					});
					Ext.create('Ext.window.Window', {
						title : 'Purchase Deals',
						id : 'recPurchResWindow',
						height : 330,
						frame : true,
						width : 500,
						constrain : true,
						resizable : false,
						layout : {
							type : 'fit'
						},
						items : [ resultWindow ]
					}).show();
				}
			} ]
		}, instrument, basic, deal, insDtl ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
				Ext.ComponentQuery.query('#recPurchDuration')[0].setText('');
				Ext.ComponentQuery.query('#recDealDealID')[0].setText('');
				Ext.ComponentQuery.query('#recDealStatusLbl')[0].setText('');
				Ext.ComponentQuery.query('#recDealListedLabel')[0].setText('');
				Ext.ComponentQuery.query('#recDealInstrumentType')[0].enable();
				Ext.ComponentQuery.query('#recDealInstrumentCodeCombo')[0].enable();
				Ext.ComponentQuery.query('#recDealInstrumentCodeText')[0].enable();
				Ext.ComponentQuery.query('#recPurchaseFund')[0].enable();
				App.Core.setScreen(PurchasesDeal.getCenterPanel());
			}
		}, {
			text : 'Save',
			id : 'recPurchSaveBtn',
			itemId : 'saveButton',
			handler : function() {
				var form = this.up('form').getForm();
				form.url = 'addNewDeal.action';
				if (form.isValid()) {
					formPanel.setLoading('Saving...');
					var couponDates = '';
					Ext.data.StoreManager.lookup('couponDatesStore').each(function(r) {
						couponDates += Ext.util.Format.date(r.get('couponDate')) + ';';
					});
					var rstDt = Ext.ComponentQuery.query('#recPurchFloatResetDt')[0];
					var valueDtField = Ext.ComponentQuery.query('#recPurchValueDt')[0];
					var maturityDtField = Ext.ComponentQuery.query('#recPurchMaturityDt')[0];
					var params = (Ext.ComponentQuery.query('#recPurchParamLbl')[0]).text;

					form.submit({
						params : {
							'couponDates' : couponDates,
							'resetDate' : Ext.util.Format.date(rstDt.getValue()),
							'valueDate' : Ext.util.Format.date(valueDtField.getValue()),
							'maturityDate' : Ext.util.Format.date(maturityDtField.getValue()),
							'user' : 'createdUser',
							'params' : params
						},
						success : function(form, action) {
							form.reset();
							(Ext.ComponentQuery.query('#recPurchParamLbl')[0]).setText('');
							Ext.ComponentQuery.query('#recPurchDuration')[0].setText('');
							Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
							Ext.Msg.alert('Success', action.result.name + ' is created.<br> Deal Code is <b>' + action.result.dealCode + '</b>');
							formPanel.setLoading(false);
							App.Core.setScreen(PurchasesDeal.getCenterPanel());
						},
						failure : function(form, action) {
							Ext.Msg.alert('Failed', 'There is some error returned from the server.<br><b>' + action.result.status + '</b>');
							formPanel.setLoading(false);
						}
					});
				} else {
					Ext.Msg.alert("Form Incomplete. Please check!");
				}
			}
		}, {
			text : 'Update',
			id : 'recPurchUpdateBtn',
			itemId : 'updateButton',
			hidden : true,
			handler : function() {
				var form = this.up('form').getForm();

				form.url = 'updatePurchase.action';

				Ext.ComponentQuery.query('#recDealInstrumentType')[0].enable();
				Ext.ComponentQuery.query('#recDealInstrumentCodeCombo')[0].enable();
				Ext.ComponentQuery.query('#recDealInstrumentCodeText')[0].enable();
				Ext.ComponentQuery.query('#recPurchaseFund')[0].enable();

				var tempInsTypeCmb = Ext.ComponentQuery.query('#recDealInstrumentType')[0];
				var tempInsType = tempInsTypeCmb.getValue();
				var record = tempInsTypeCmb.store.getById(tempInsType);
				tempInsTypeCmb.select(tempInsType);
				tempInsTypeCmb.fireEvent('select', tempInsTypeCmb, record);

				if (form.isValid()) {
					formPanel.setLoading('Updating...');
					var couponDates = '';
					Ext.data.StoreManager.lookup('couponDatesStore').each(function(r) {
						couponDates += Ext.util.Format.date(r.get('couponDate')) + ';';
					});
					var rstDt = Ext.ComponentQuery.query('#recPurchFloatResetDt')[0];
					var valueDtField = Ext.ComponentQuery.query('#recPurchValueDt')[0];
					var maturityDtField = Ext.ComponentQuery.query('#recPurchMaturityDt')[0];
					var dealCode = Ext.ComponentQuery.query('#recDealDealID')[0].text.substr(9);
					var params = (Ext.ComponentQuery.query('#recPurchParamLbl')[0]).text;

					form.submit({
						params : {
							'couponDates' : couponDates,
							'resetDate' : Ext.util.Format.date(rstDt.getValue()),
							'valueDate' : Ext.util.Format.date(valueDtField.getValue()),
							'maturityDate' : Ext.util.Format.date(maturityDtField.getValue()),
							'dealCode' : dealCode,
							'user' : 'updateUser',
							'params' : params
						},
						success : function(form, action) {
							form.reset();
							(Ext.ComponentQuery.query('#recPurchParamLbl')[0]).setText('');
							Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
							Ext.Msg.alert('Success', action.result.name + ' is Updated.<br> Deal Code is <b>' + action.result.dealCode + '</b>');
							formPanel.setLoading(false);
							App.Core.setScreen(PurchasesDeal.getCenterPanel());
						},
						failure : function(form, action) {
							Ext.Msg.alert('Failed', 'There is some error returned from the server.<br><b>' + action.result.status + '</b>');
							formPanel.setLoading(false);
						}
					});

				} else {
					Ext.Msg.alert("Form Incomplete. Please check!");
				}
			}
		} ]
	});

	return formPanel;
};

PurchasesDeal.getRollOverDeal = function(instumentType, fund, dealCode, issuer, valueDate, principleValue) {

	var panel = PurchasesDeal.getCenterPanel();
	var tmpInsTypeStore = Ext.data.StoreManager.lookup('recPurchInsTypeStore');
	var tmpFundStore = Ext.data.StoreManager.lookup('dealFundsStore');
	var tmpPrimeDealerStore = Ext.data.StoreManager.lookup('primaryDealersStore');
	var tmpIssuerStore = Ext.data.StoreManager.lookup('issuersStore');

	var tmpPrimDealerCmb = Ext.getCmp('recordDealPrimDealer');
	var tmpIssuerCmb = Ext.getCmp('recordDealIssuer');
	var insTypeCmb = Ext.getCmp('recDealInstrumentType');
	var fundCmb = Ext.getCmp('recPurchaseFund');
	var tmpValDt = Ext.getCmp('recPurchValueDt');
	var tmpDealValue = Ext.getCmp('recPurchDealVal');
	var paramLabel = Ext.getCmp('recPurchParamLbl');

	Ext.Ajax.request({
		url : 'getInstrumentTypes.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			tmpInsTypeStore.loadRawData(obj.instrumentTypeList);
			insTypeCmb.setValue(tmpInsTypeStore.getAt(tmpInsTypeStore.find('instrumentName', instumentType)).get('id'));
			var insTypeRecord = tmpInsTypeStore.findRecord('instrumentName', instumentType);
			insTypeCmb.fireEvent('select', insTypeCmb, insTypeRecord);
			Ext.Ajax.request({
				url : 'getFundsForDealInsType.action',
				params : {
					querry : 'PURCH',
					querryTwo : insTypeCmb.getValue()
				},
				success : function(response) {
					var obj = Ext.decode(response.responseText);
					Ext.data.StoreManager.lookup('dealFundsStore').loadRawData(obj.fundList);
					fundCmb.setValue(tmpFundStore.getAt(tmpFundStore.find('fundCode', fund)).get('id'));
					if (!tmpPrimDealerCmb.isDisabled()) {
						tmpPrimDealerCmb.setValue(tmpPrimeDealerStore.getAt(tmpPrimeDealerStore.find('code', issuer)).get('id'));
					} else {
						tmpIssuerCmb.setValue(tmpIssuerStore.getAt(tmpIssuerStore.find('code', issuer)).get('id'));
					}
				},
				failure : function(response) {
					Ext.Msg.alert('Error!', 'Failed to Load Fund!');
				}
			});
		},
		failure : function(response) {
			Ext.Msg.alert('Error!', 'Failed to Load Instrument Type!');
		}
	});
	tmpValDt.setValue(valueDate);
	tmpDealValue.setValue(principleValue);
	paramLabel.setText(dealCode);

	return panel;
};

PurchasesDeal.setMatVal = function(withholdingTaxTypeFrom) {

	// Ext.ComponentQuery.query('#recDealMaturityValue')[0].reset();
	console.log('Start SetMatVal');

	var withholdingTaxType = withholdingTaxTypeFrom;

	var insType = Ext.ComponentQuery.query('#recDealInstrumentType')[0].getValue();
	if (insType == null)
		return;

	console.log('insType: ' + insType);
	var valueDt = Ext.ComponentQuery.query('#recPurchValueDt')[0].getValue();
	if (valueDt == null)
		return;
	console.log('valueDt :' + valueDt);

	var maturityDt = Ext.ComponentQuery.query('#recPurchMaturityDt')[0].getValue();
	if (maturityDt == null)
		return;
	console.log('maturityDt :' + maturityDt);

	if (withholdingTaxType == null || withholdingTaxType == '')
		withholdingTaxType = Ext.ComponentQuery.query('#recDealWhTMat')[0].getGroupValue();
	var withholdingTaxRate = Ext.ComponentQuery.query('#withholdingTaxRate')[0].getValue();

	console.log('withholdingTaxType :' + withholdingTaxType);
	console.log('withholdingTaxRate :' + withholdingTaxRate);

	if (withholdingTaxType == null || withholdingTaxType == '') {
		return;
	} else if (withholdingTaxType == 'Maturity' || withholdingTaxType == 'Upfront') {
		if (withholdingTaxRate == null || withholdingTaxRate == '')
			return;
	}

	var principleValueField = Ext.ComponentQuery.query('#recPurchDealVal')[0];
	var principleValue;

	console.log('PricipleVal Disabled :' + principleValueField.isDisabled());
	if (principleValueField.isDisabled()) {
		principleValue = 0;
	} else {
		principleValue = principleValueField.getValue();
		if (principleValue == '' || principleValue == null)
			return;
	}
	console.log('principleValue :' + principleValue);

	var maturityValueField = Ext.ComponentQuery.query('#recDealMaturityValue')[0];
	var maturityValue;

	console.log('maturityVal Disabled :' + maturityValueField.isDisabled());
	if (maturityValueField.isDisabled()) {
		maturityValue = 0;
	} else {
		maturityValue = maturityValueField.getValue();
		if (maturityValue == '' || maturityValue == null)
			return;
	}
	console.log('maturityValue :' + maturityValue);

	var interestRate = Ext.ComponentQuery.query('#recPurchInterestRate')[0].getValue();
	if (interestRate == null)
		return;
	console.log('interestRate :' + interestRate);

	var dayBasis = Ext.ComponentQuery.query('#recDealDayBasis')[0].getValue();
	if (dayBasis == null)
		return;
	console.log('dayBasis :' + dayBasis);

	console.log('COMPLETED');
	Ext.Ajax.request({
		url : 'evalPurchase.action',
		params : {
			'insType' : insType,
			'valueDate' : Ext.util.Format.date(valueDt),
			'maturityDate' : Ext.util.Format.date(maturityDt),
			'withholdingTax' : withholdingTaxType,
			'withholdingTaxRate' : withholdingTaxRate,
			'dealValue' : principleValue,
			'maturityValue' : maturityValue,
			'dayBasis' : dayBasis,
			'interestRate' : interestRate
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			console.log('Returned Value : ' + obj.maturityValue);
			Ext.ComponentQuery.query('#recDealMaturityValue')[0].setValue(Ext.util.Format.number(obj.maturityValue.replace(/,/g, ''), '0,000.0000'));
		},
		failure : function(response) {
			alert('Error in Maturity Value Calculation : ' + obj.status);
			Ext.ComponentQuery.query('#recDealMaturityValue')[0].reset();
		}
	});
};

// /************************************************
ConfPurchase = new Object();

ConfPurchase.getBasicFieldSet = function() {

	var funds = Ext.create('Ext.data.Store', {
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'fundName',
			type : 'string',
		}, {
			name : 'fundCode',
			type : 'string',
		} ],
		storeId : 'dealFundsStore',
	});

	return Ext.create('Ext.form.FieldSet', {
		title : 'Basic',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'label',
			id : 'confirmPurchParamLbl',
			hidden : true,
			text : '',
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ Ext.create('Ext.form.ComboBox', {
				name : 'fund',
				fieldLabel : 'Fund',
				store : funds,
				queryMode : 'local',
				flex : 1,
				id : 'confirmPurchFund',
				padding : '0 5 5 0',
				valueField : 'id',
				displayField : 'fundName',
				disabled : true,
				autoScroll : true,
				allowBlank : false,
				blankText : 'Please select the Fund',
				msgTarget : 'under',
				editable : false,
			}), {
				xtype : 'textfield',
				name : 'name',
				id : 'confirmPurchInsName',
				fieldLabel : 'Name',
				disabled : true,
				flex : 1,
				allowBlank : false,
				padding : '0 5 5 0',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'datefield',
				fieldLabel : 'Value Date',
				id : 'confirmPurchValueDt',
				allowBlank : false,
				disabled : true,
				flex : 1,
				padding : '0 5 5 0',
				blankText : 'Please select a Value Date',
				msgTarget : 'under',
				format : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				// SSS minValue : new Date(),
				listeners : {
					'change' : {
						fn : function(field, newVal, oldVal) {
							var dfMaturityDate = Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];
							if (dfMaturityDate.getValue() != null) {
								var lblMaturityDate = Ext.ComponentQuery.query('#confirmPurchDuration')[0];
								if (this.getValue() > dfMaturityDate.getValue()) {
									Ext.MessageBox.alert('Error', 'Value Date cannot be greater than Maturity Date.');
									this.setValue(null);
									lblMaturityDate.setText('');
								} else {
									var durationDays = (dfMaturityDate.getValue() - this.getValue()) / 86400000;
									lblMaturityDate.setText('Duration: '.concat(durationDays.toString().concat(' days.')));
								}
							}
						}
					}
				}

			}, {
				xtype : 'datefield',
				fieldLabel : 'Matured Date',
				id : 'confirmPurchMaturityDt',
				allowBlank : false,
				disabled : true,
				flex : 1,
				padding : '0 5 5 0',
				blankText : 'Please select a Matured Date',
				msgTarget : 'under',
				format : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				// minValue : new Date(),
				listeners : {
					'change' : {
						fn : function(field, newVal, oldVal) {
							var dfValueDate = Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
							if (dfValueDate.getValue() != null) {
								var lblValueDate = Ext.ComponentQuery.query('#confirmPurchDuration')[0];
								if (dfValueDate.getValue() > this.getValue()) {
									Ext.MessageBox.alert('Error', 'Maturity Date cannot be smaller than Value Date.');
									this.setValue(null);
									lblValueDate.setText('');
								} else {
									var durationDays = (this.getValue() - dfValueDate.getValue()) / 86400000;
									lblValueDate.setText('Duration: '.concat(durationDays.toString().concat(' days.')));
								}

							}
						}
					}
				}
			} ]
		}, {
			xtype : 'label',
			id : 'confirmPurchDuration'
		}, , {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'combo',
				fieldLabel : 'Delevary Method',
				store : CommenStore.delevaryMethodStore,
				valueField : 'id',
				flex : 1,
				id : 'confPurchaseDelevaryMethod',
				labelWidth : 120,
				name : 'delevarymethode',
				padding : '0 5 0 0',
				disabled : true,
				displayField : 'code',
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{description}</b><br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {description}', '</tpl>'),
				listeners : {
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
							combo.setValue(null);
						}
					}
				}
			}, {
				xtype : 'combo',
				fieldLabel : 'Dealer',
				store : CommenStore.dealersStore,
				valueField : 'id',
				flex : 1,
				id : 'confPurchaseDealer',
				padding : '0 5 0 0',
				name : 'dealerCode',
				disabled : true,
				displayField : 'code',
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{name}</b><br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {name}', '</tpl>'),
				listeners : {
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
							combo.setValue(null);
						}
					}
				}
			} ]
		}, {
			xtype : 'combo',
			labelWidth : 120,
			fieldLabel : 'Settlement Method',
			store : CommenStore.settlmentStore,
			valueField : 'id',
			id : 'confPurchaseSettlementMethod',
			anchor : '50%',
			name : 'settlementCode',
			padding : '0 10 0 0',
			displayField : 'code',
			disabled : true,
			tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{description}</b><br/></div>', '</tpl>'),
			displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {description}', '</tpl>'),
			listeners : {
				change : function(combo, record, index) {
					if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
						combo.setValue(null);
					}
				}
			}
		} ]
	});
};

ConfPurchase.getInstrumentFieldSet = function() {
	Ext.define('InstrumentPurch', {
		extend : 'Ext.data.Model',
		fields : [ {
			name : 'code',
			type : 'string'
		}, {
			name : 'type',
			type : 'string'
		}, {
			name : 'issuer',
			type : 'string'
		}, {
			name : 'status',
			type : 'string'
		} ]
	});

	var instrumentStore = new Ext.data.Store({
		model : 'InstrumentPurch',
		storeId : 'confirmPurchInsStore',
	});

	Ext.Ajax.request({
		url : 'getInstruments.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			var arrayLength = obj.instruemtList.length;
			for (var i = 0; i < arrayLength; i++) {
				instrumentStore.add({
					code : obj.instruemtList[i].instrumentCode,
					type : obj.instruemtList[i].instrumentType.instrumentName,
					issuer : obj.instruemtList[i].issuer.code,
					status : obj.instruemtList[i].status,
				});
			}
		}
	});

	Ext.define('InstrumentTypePurch', {
		extend : 'Ext.data.Model',
		fields : [ {
			name : 'id',
			type : 'int'
		}, {
			name : 'instrumentName',
			type : 'string'
		}, {
			name : 'istrumentType',
			type : 'int'
		}, {
			name : 'scripts',
			type : 'int'
		}, {
			name : 'denominals',
			type : 'int'
		}, {
			name : 'listed',
			type : 'int'
		}, {
			name : 'withholdingTax',
			type : 'string'
		}, {
			name : 'interestRate',
			type : 'string'
		}, {
			name : 'interestType',
			type : 'string'
		}, {
			name : 'dayBasis',
			type : 'string'
		}, {
			name : 'couponAvailability',
			type : 'int'
		}, {
			name : 'cuponFrequency',
			type : 'int'
		}, {
			name : 'shortMaturable',
			type : 'int'
		} ]
	});

	var instrumentTypeStore = new Ext.data.Store({
		model : 'InstrumentTypePurch',
		storeId : 'confirmPurchInsTypeStore',
	});

	Ext.Ajax.request({
		url : 'getInstrumentTypes.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			instrumentTypeStore.loadRawData(obj.instrumentTypeList);
		}
	});

	return Ext.create('Ext.form.FieldSet', {
		title : 'Instrument',
		defaults : {
			anchor : '100%'
		},
		items : [
				{
					xtype : 'fieldcontainer',
					layout : 'hbox',
					items : [
							{
								xtype : 'combo',
								name : 'insType',
								id : 'confirmPurchInstrumentType',
								fieldLabel : 'Instrument Type',
								disabled : true,
								queryMode : 'local',
								store : instrumentTypeStore,
								valueField : 'id',
								displayField : 'instrumentName',
								flex : 1,
								padding : '0 5 0 0',
								autoScroll : true,
								allowBlank : false,
								blankText : 'Select the Instrument Type',
								msgTarget : 'under',
								editable : false,
								listeners : {
									select : {
										fn : function(combo, value) {
											var cbGov = Ext.ComponentQuery.query('#cbIssuerGovernment')[0];
											var cbOth = Ext.ComponentQuery.query('#cbIssuerOther')[0];
											var tfOth = Ext.ComponentQuery.query('#recordDealIssuer')[0];
											var cbPrimaryDealr = Ext.ComponentQuery.query('#recordDealPrimDealer')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('istrumentType') == 1) {
												cbGov.setValue(true);
												cbOth.setValue(false);
												// tfOth.clearValue();
												tfOth.disable();
												// cbPrimaryDealr.clearValue();
												cbPrimaryDealr.enable();
												cbPrimaryDealr.allowBlank = false;
												tfOth.allowBlank = true;
											} else {
												cbGov.setValue(false);
												cbOth.setValue(true);
												// tfOth.clearValue();
												tfOth.enable();
												// cbPrimaryDealr.clearValue();
												cbPrimaryDealr.disable();
												tfOth.allowBlank = false;
												cbPrimaryDealr.allowBlank = true;
											}
											var fcpn = Ext.ComponentQuery.query('#confirmPurchCouponField')[0];
											var tfcpnRt = Ext.ComponentQuery.query('#confirmPurchCouponFreq')[0];
											var fscpnRt = Ext.ComponentMgr.get('confirmPurchCoupnData');
											var cbCpnDtFix = Ext.ComponentQuery.query('#confirmPurchCpnFixcb')[0];
											var cbCpnDtVar = Ext.ComponentQuery.query('#confirmPurchCpnVarycb')[0];
											var cbCpnDtNo = Ext.ComponentQuery.query('#confirmPurchCpnNo')[0];
											var dpCpnDts = Ext.ComponentQuery.query('#confirmPurchCpnDts')[0];
											var pCpnDtStore = Ext.ComponentQuery.query('#recordDealDtStore')[0];
											var nfCouponRate = Ext.ComponentQuery.query('#confirmPurchCouponRate')[0];
											if ((instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('couponAvailability') == 1)
													|| (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('couponAvailability') == 2)) {
												fcpn.enable();
												fscpnRt.show();
												fscpnRt.enable();
												// nfCouponRate.reset(),
												nfCouponRate.enable();
												if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('couponAvailability') === 1) {
													cbCpnDtFix.setValue(true);
													cbCpnDtVar.setValue(false);
													cbCpnDtNo.setValue(false);
													tfcpnRt.setValue(instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('cuponFrequency'));
													fscpnRt.show();
													fscpnRt.enable();
													tfcpnRt.show();
													dpCpnDts.hide();
													pCpnDtStore.hide();
												} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('couponAvailability') === 2) {
													fscpnRt.show();
													fscpnRt.enable();
													cbCpnDtFix.setValue(false);
													cbCpnDtVar.setValue(true);
													cbCpnDtNo.setValue(false);
													tfcpnRt.hide();
													dpCpnDts.show();
													pCpnDtStore.show();
												} else {
													fscpnRt.hide();
													fscpnRt.disable();
													cbCpnDtFix.setValue(false);
													cbCpnDtVar.setValue(false);
													cbCpnDtNo.setValue(true);
													tfcpnRt.hide();
													dpCpnDts.hide();
													pCpnDtStore.hide();
												}
												// tfcpnRt.disable();
											} else {
												fscpnRt.hide();
												fscpnRt.disable();
												fcpn.disable();
												cbCpnDtFix.setValue(false);
												cbCpnDtVar.setValue(false);
												cbCpnDtNo.setValue(true);
												tfcpnRt.hide();
												dpCpnDts.hide();
												pCpnDtStore.hide();
												// nfCouponRate.reset();
												nfCouponRate.disable();
											}

											var cbIntTypeFix = Ext.ComponentQuery.query('#confirmPurchIntTypeFix')[0];
											var cbIntTypeFloat = Ext.ComponentQuery.query('#confirmPurchIntTypeFloat')[0];
											// var fsFloatField1 =
											// Ext.ComponentQuery.query('#confirmPurchFloatField1')[0];
											var rstPrd = Ext.ComponentQuery.query('#RecPurchResetPeriod')[0];
											var rstDt = Ext.ComponentQuery.query('#confirmPurchFloatResetDt')[0];
											var rstBench = Ext.ComponentQuery.query('#confirmPurchBench')[0];
											var rstFlr = Ext.ComponentQuery.query('#confirmPurchFloor')[0];
											var rstClng = Ext.ComponentQuery.query('#confirmPurchCeiling')[0];
											// var fsFloatField2 =
											// Ext.ComponentQuery.query('#confirmPurchFloatField2')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('interestRate') == 'Fixed') {
												cbIntTypeFix.setValue(true);
												cbIntTypeFloat.setValue(false);
												// fsFloatField1.disable();
												rstPrd.disable();
												rstDt.disable();
												rstBench.disable();
												rstFlr.disable();
												rstClng.disable();
												// fsFloatField1.hide();
												rstPrd.hide();
												rstDt.hide();
												rstBench.hide();
												rstFlr.hide();
												rstClng.hide();
												// fsFloatField2.disable();
												// fsFloatField2.hide();
											} else {
												cbIntTypeFix.setValue(false);
												cbIntTypeFloat.setValue(true);
												// fsFloatField1.enable();
												rstPrd.enable();
												rstDt.enable();
												rstBench.enable();
												rstFlr.enable();
												rstClng.enable();
												// fsFloatField1.show();
												rstPrd.show();
												rstDt.show();
												rstBench.show();
												rstFlr.show();
												rstClng.show();
												// fsFloatField2.enable();
												// fsFloatField2.show();
											}

											var rbNoWhT = Ext.ComponentQuery.query('#confirmPurchWhTNo')[0];
											var rbUpWhT = Ext.ComponentQuery.query('#confirmPurchWhTUp')[0];
											var rbMatWhT = Ext.ComponentQuery.query('#confirmPurchWhTMat')[0];

											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('withholdingTax') === 'No') {
												rbNoWhT.setValue(true);
												rbUpWhT.setValue(false);
												rbMatWhT.setValue(false);
												rbNoWhT.disable();
												rbUpWhT.disable();
												rbMatWhT.disable();
											} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('withholdingTax') === 'Upfront') {
												rbNoWhT.setValue(false);
												rbUpWhT.setValue(true);
												rbMatWhT.setValue(false);
												rbNoWhT.disable();
												rbUpWhT.disable();
												rbMatWhT.disable();
											} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('withholdingTax') === 'Maturity') {
												rbNoWhT.setValue(false);
												rbUpWhT.setValue(false);
												rbMatWhT.setValue(true);
												rbNoWhT.disable();
												rbUpWhT.disable();
												rbMatWhT.disable();
											} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('withholdingTax') === 'Vary') {
												rbNoWhT.setValue(false);
												rbUpWhT.setValue(false);
												rbMatWhT.setValue(false);
												rbNoWhT.enable();
												rbUpWhT.enable();
												rbMatWhT.enable();
											}

											var comDayBas = Ext.ComponentQuery.query('#confirmPurchDayBasis')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('dayBasis') == 'Vary') {
												comDayBas.enable();
											} else {
												comDayBas.setValue(instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('dayBasis'));
												comDayBas.disable();
											}

											var cbScripts = Ext.ComponentQuery.query('#confirmPurchScripts')[0];
											var cbDenominals = Ext.ComponentQuery.query('#confirmPurchdenominals')[0];
											var tfMinDenomi = Ext.ComponentQuery.query('#confirmPurchDenominamMinVal')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('scripts') == 1) {
												cbScripts.setValue(true);
												if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('denominals') == 1) {
													cbDenominals.setValue(true);
													tfMinDenomi.enable();
												} else {
													cbDenominals.setValue(false);
													tfMinDenomi.disable();
												}
											} else {
												cbScripts.setValue(false);
												cbDenominals.setValue(false);
												// tfMinDenomi.reset();
												tfMinDenomi.disable();
											}

											var rbStraight = Ext.ComponentQuery.query('#confirmPurchIntCalStr')[0];
											var rbDiscounted = Ext.ComponentQuery.query('#confirmPurchIntCalDis')[0];
											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('interestType') === 'Discounted') {
												rbStraight.setValue(false);
												rbDiscounted.setValue(true);
											} else if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('interestType') === 'Straight') {
												rbStraight.setValue(true);
												rbDiscounted.setValue(false);
											} else {
												rbStraight.setValue(false);
												rbDiscounted.setValue(false);
												Ext.Msg.alert('Error', 'Invalid Interest Calculation Method Found!');
											}

											var lblListed = Ext.ComponentQuery.query('#confirmPurchListedLabel')[0];
											var comboInsCode = Ext.ComponentQuery.query('#confirmPurchInstrumentCodeCombo')[0];
											var tfInsCode = Ext.ComponentQuery.query('#confirmPurchInstrumentCodeText')[0];

											if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('listed') === 1) {
												lblListed.setText('Listed Instrument');
												instrumentStore.clearFilter(true);
												// instrumentStore.filter('type',
												// instrumentTypeStore.getAt(instrumentTypeStore.find('id',
												// combo.getValue())).get('instrumentName'));
												instrumentStore.filterBy(function(rec, id) {
													if (rec.get('type') === instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue())).get('instrumentName'))
														return true;
													else
														return false;
												});
												comboInsCode.bindStore(instrumentStore);
												tfInsCode.disable();
												tfInsCode.hide();
												// comboInsCode.clearValue();
												comboInsCode.show();
												comboInsCode.enable();
											} else {
												lblListed.setText('Non-Listed Instrument');
												comboInsCode.hide();
												comboInsCode.disable();
												tfInsCode.show();
												tfInsCode.enable();
												var tfInsName = Ext.ComponentQuery.query('#confirmPurchInsName')[0];
												// tfInsName.reset();
												tfInsName.enable();
												var dfValDate = Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
												// dfValDate.reset();
												dfValDate.enable();
												var dfMatDate = Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];
												// dfMatDate.reset();
												dfMatDate.enable();
											}

											Ext.Ajax.request({
												url : 'getFundsForDealInsType.action',
												params : {
													querry : 'PURCH',
													querryTwo : combo.getValue(),
												},
												success : function(response) {
													var obj = Ext.decode(response.responseText);
													Ext.data.StoreManager.lookup('dealFundsStore').loadRawData(obj.fundList);
												}
											});
										}
									}
								}
							}, {
								xtype : 'label',
								flex : 1,
								id : 'confirmPurchListedLabel',
								style : 'font-weight:600;',
							} ]
				}, Ext.create('Ext.form.ComboBox', {
					name : 'listedInsCode',
					id : 'confirmPurchInstrumentCodeCombo',
					fieldLabel : 'Instrument Code',
					store : instrumentStore,
					queryMode : 'local',
					valueField : 'code',
					displayField : 'code',
					anchor : '50%',
					padding : '0 5 0 0',
					style : 'margin-top:10px;',
					disabled : true,
					autoScroll : true,
					allowBlank : false,
					blankText : 'Select the Instrument Code',
					msgTarget : 'under',
					editable : false,
					hidden : true,
					tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{issuer}</b><br/></div>', '</tpl>'),
					displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code}', '</tpl>'),
					listeners : {
						select : {
							fn : function(combo, value) {
								var comboInsType = Ext.ComponentQuery.query('#confirmPurchInstrumentType')[0];

								if (instrumentTypeStore.getAt(instrumentTypeStore.find('id', comboInsType.getValue())).get('Listed') === 1) {
									var tfInsName = Ext.ComponentQuery.query('#confirmPurchInsName')[0];
									tfInsName.setValue(instrumentStore.getAt(instrumentStore.find('code', combo.getValue())).get('name'));
									tfInsName.disable();

									var dfValDate = Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
									dfValDate.format = 'd/m/Y';
									dfValDate.setValue(instrumentStore.getAt(instrumentStore.find('code', combo.getValue())).get('valueDate'));
									dfValDate.disable();

									var dfMatDate = Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];
									dfMatDate.format = 'd/m/Y';
									dfMatDate.setValue(instrumentStore.getAt(instrumentStore.find('code', combo.getValue())).get('maturityDate'));
									dfMatDate.disable();
								}
							}
						}
					}
				}), {
					xtype : 'textfield',
					name : 'insCode',
					fieldLabel : 'Instrument Code',
					id : 'confirmPurchInstrumentCodeText',
					allowBlank : false,
					disabled : true,
					anchor : '50%',
					padding : '0 5 0 0',
					style : 'margin-top:10px;',
					blankText : 'Instrument Code is required',
					msgTarget : 'under',
					listeners : {
						'blur' : function(field) {
							find = instrumentStore.find('code', field.getValue());
							if (find != -1) {
								Ext.Msg.alert(field.getValue() + " is already used!");
								this.reset();
							}
						}
					}
				}, {
					xtype : 'label',
					flex : 1,
					id : 'confirmPurchDealID',
					style : 'font-weight:600;margin-top:15px;',
					text : 'Deal ID: ',
					hidden : true,
				}, {
					xtype : 'label',
					flex : 1,
					id : 'confirmPurchStatusLbl',
					style : 'font-weight:600;margin-top:15px;',
					hidden : true,
				} ]
	});
};

ConfPurchase.getDealFieldSet = function() {
	return Ext.create('Ext.form.FieldSet', {
		title : 'Deal',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				id : 'confirmPurchDealVal',
				minValue : 0,
				name : 'dealValue',
				fieldLabel : 'Deal Value',
				allowBlank : false,
				disabled : true,
				blankText : 'Please enter the Deal Amount',
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			}, {
				xtype : 'textfield',
				minValue : 0,
				name : 'placementFee',
				id : 'confirmPurchPlacementFee',
				fieldLabel : 'Placement Fee',
				allowBlank : false,
				disabled : true,
				blankText : 'Please enter the Placement Fee',
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'checkboxfield',
				boxLabel : 'Secondary Market',
				id : 'confirmPurchSecMarket',
				name : 'secMarket',
				inputValue : 1,
				flex : 1,
				padding : '0 5 5 0',
				listeners : {
					change : {
						fn : function(object, Value) {
							if (Value) {
								var ssname = Ext.ComponentQuery.query('#secSellerName')[0];
								ssname.enable();
								var ssbank = Ext.ComponentQuery.query('#secSellerBank')[0];
								ssbank.enable();
							} else {
								var ssname = Ext.ComponentQuery.query('#secSellerName')[0];
								ssname.disable();
								var ssbank = Ext.ComponentQuery.query('#secSellerBank')[0];
								ssbank.disable();
							}
						}
					}
				}
			}, {
				xtype : 'textfield',
				minValue : 0,
				id : 'confirmPurchMaturityValue',
				fieldLabel : 'Maturity Value',
				// allowBlank : false,
				name : 'maturityValue',
				disabled : true,
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value.replace(/,/g, ''), '0,000.0000'));
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			}

			]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			style : 'margin-top:10px;',
			items : [ {
				xtype : 'textfield',
				name : 'secSellerName',
				id : 'secSellerName',
				fieldLabel : "Seller's Name",
				disabled : true,
				flex : 1,
				padding : '0 5 5 0',
			}, {
				xtype : 'textfield',
				name : 'secSellerBankNo',
				id : 'secSellerBank',
				fieldLabel : "Bank No",
				disabled : true,
				flex : 1,
				padding : '0 5 5 0',
			} ]
		} ]
	});
};

ConfPurchase.getInstrumentDetailFieldSet = function() {

	var cpnDtStore = Ext.create('Ext.data.Store', {
		storeId : 'couponDatesStore',
		fields : [ {
			name : 'couponDate',
			type : 'date',
			dateFormat : 'd/m/Y'
		} ]
	});

	var primaryDealerStore = Ext.create('Ext.data.Store', {
		storeId : 'primaryDealersStore',
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'name',
			type : 'string',
		}, {
			name : 'code',
			type : 'string',
		}, {
			name : 'creditRating',
			type : 'string',
		} ]
	});

	var issuersStore = Ext.create('Ext.data.Store', {
		storeId : 'issuersStore',
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'name',
			type : 'string',
		}, {
			name : 'code',
			type : 'string',
		}, {
			name : 'creditRating',
			type : 'string',
		} ]
	});

	Ext.Ajax.request({
		url : 'getPrimaryDealers.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			primaryDealerStore.loadRawData(obj.issuers);
		}
	});

	Ext.Ajax.request({
		url : 'getIssuers.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			issuersStore.loadRawData(obj.issuerList);
		}
	});

	var valDate = Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
	var matDate = Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];

	return Ext.create('Ext.form.FieldSet', {
		title : 'Instrument Details',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'fieldcontainer',
				labelWidth : 115,
				fieldLabel : 'Instrument Type',
				defaultType : 'radiofield',
				flex : 1,
				padding : '0 5px 0 0',
				layout : 'vbox',
				items : [ {
					boxLabel : 'Government',
					padding : '0 40 0 0',
					id : 'cbIssuerGovernment',
					disabled : true
				}, {
					boxLabel : 'Other',
					id : 'cbIssuerOther',
					disabled : true,
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'vbox',
				flex : 1,
				padding : '0 5px 0 0',
				items : [ Ext.create('Ext.form.ComboBox', {
					fieldLabel : 'Primary Dealer',
					store : primaryDealerStore,
					valueField : 'id',
					name : 'primaryDealer',
					queryMode : 'local',
					id : 'recordDealPrimDealer',
					autoScroll : true,
					editable : false,
					disabled : true,
					// flex : 1,
					padding : '0 10px 0 0',
					tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item"><b>{code}</b>-{name}-<b>{creditRating}</b><br/></div>', '</tpl>'),
					displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{name}', '</tpl>'),

				}), Ext.create('Ext.form.ComboBox', {
					fieldLabel : 'Issuer',
					store : issuersStore,
					valueField : 'id',
					name : 'issuer',
					// displayField : 'name',
					queryMode : 'local',
					id : 'recordDealIssuer',
					autoScroll : true,
					editable : false,
					disabled : true,
					padding : '0 10px 0 0',
					tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item"><b>{code}</b>-{name}-<b>{creditRating}</b><br/></div>', '</tpl>'),
					displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{name}', '</tpl>'),
				}) ]
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			style : 'margin-top:10px;',
			items : [ {
				xtype : 'fieldcontainer',
				labelWidth : 115,
				fieldLabel : 'Coupon Availability',
				defaultType : 'radiofield',
				id : 'confirmPurchCouponField',
				flex : 1,
				layout : 'hbox',
				items : [ {
					boxLabel : 'No',
					id : 'confirmPurchCpnNo',
					disabled : true
				}, {
					boxLabel : 'Fixed',
					id : 'confirmPurchCpnFixcb',
					disabled : true
				}, {
					boxLabel : 'Vary',
					id : 'confirmPurchCpnVarycb',
					disabled : true
				} ]
			}, {
				xtype : 'numberfield',
				minValue : 0,
				maxValue : 100,
				decimalPrecision : 4,
				flex : 1,
				padding : '0 5px 0 0',
				name : 'couponRate',
				id : 'confirmPurchCouponRate',
				fieldLabel : 'Coupon Rate',
				// *allowBlank : false,
				blankText : 'Coupon Rate is required',
				msgTarget : 'under',
				disabled : true,
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			hidden : true,
			id : 'confirmPurchCoupnData',
			style : 'margin-top:10px;',
			items : [ {
				xtype : 'numberfield',
				minValue : 0,
				labelWidth : 115,
				maxValue : 100,
				decimalPrecision : 0,
				// padding : '0 0 5 0',
				name : 'couponFreq',
				anchor : '60%',
				id : 'confirmPurchCouponFreq',
				fieldLabel : 'Coupon Frequency',
				disabled : true,
				hidden : true,
				allowBlank : false,
			}, {
				xtype : 'datefield',
				fieldLabel : 'Coupon Dates',
				id : 'confirmPurchCpnDts',
				minValue : new Date(),
				flex : 1,
				padding : '0 5px 0 0',
				labelWidth : 115,
				allowBlank : false,
				blankText : 'Please select a Start Date',
				msgTarget : 'under',
				format : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				hidden : true,
				listeners : {
					change : {
						fn : function(object, newVal, oldVal) {

							var recordIndex = cpnDtStore.findBy(function(record, id) {
								if (newVal != null && (record.get('couponDate').getTime() == newVal.getTime())) {
									return true;
								}
								return false;
							});
							if (recordIndex != -1) {
								Ext.MessageBox.alert('Error!', 'Duplicate Entry!!!');
							} else if (newVal == null) {
								console.log("Null value for date field");
							} else if (valDate.getValue() == null || matDate.getValue() == null) {
								Ext.MessageBox.alert('Error!', 'Deal Period not specified!');
							} else if (newVal < valDate.getValue() || newVal > matDate.getValue()) {
								Ext.MessageBox.alert('Error!', 'Coupon Date should be within the Deal Period!');
							} else {
								cpnDtStore.add({
									couponDate : Ext.util.Format.date(newVal, 'd/m/Y')
								});
							}
						}
					}
				}
			}, Ext.create('Ext.grid.Panel', {
				width : 100,
				height : 130,
				flex : 1,
				store : cpnDtStore,
				id : 'recordDealDtStore',
				autoScroll : true,
				columns : [ {
					xtype : 'datecolumn',
					header : 'Coupon Dates',
					dataIndex : 'couponDate',
					format : 'd/m/Y',
					flex : 1
				} ],
				hidden : true,
				bbar : [ {
					xtype : 'button',
					text : 'Remove',
					handler : function() {
						var pCpnDtStore = Ext.ComponentQuery.query('#recordDealDtStore')[0];
						var sm = pCpnDtStore.getSelectionModel();
						var sel = sm.getSelection();
						var i = 0;
						var currentDate = new Date();
						for (i = 0; i < sel.length; i++) {
							if (sel[i].get("couponDate") > currentDate) {
								pCpnDtStore.store.remove(sel[i]);
							} else {
								Ext.Msg.alert('Error', 'Cannot Remove Previous Dates!');
							}
						}
					}
				} ]
			}) ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'fieldcontainer',
				fieldLabel : 'Interest Type',
				defaultType : 'radiofield',
				layout : 'hbox',
				flex : 1,
				labelWidth : 115,
				style : 'margin-top:10px;',
				items : [ {
					boxLabel : 'Fixed',
					id : 'confirmPurchIntTypeFix',
					disabled : true
				}, {
					boxLabel : 'Floating',
					id : 'confirmPurchIntTypeFloat',
					disabled : true
				} ]
			}, {
				xtype : 'numberfield',
				minValue : 0,
				maxValue : 100,
				decimalPrecision : 4,
				padding : '0 5px 0 0',
				name : 'interestRate',
				fieldLabel : 'Rate',
				style : 'margin-top:10px;',
				flex : 1,
				id : 'confirmPurchInterestRate',
				allowBlank : false,
				blankText : 'Rate is required',
				msgTarget : 'under',
				disabled : true,
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			id : 'confirmPurchFloatField1',
			// hidden : true,

			items : [ {
				xtype : 'numberfield',
				id : 'RecPurchResetPeriod',
				labelWidth : 115,
				fieldLabel : 'Reset Period',
				name : 'rateResetPeriod',
				flex : 1,
				hidden : true,
				minValue : 1,
				decimalPrecision : 0,
				allowBlank : false,
				padding : '0 79px 0 0'
			}, {
				xtype : 'datefield',
				fieldLabel : 'Reset Date',
				id : 'confirmPurchFloatResetDt',
				flex : 1,
				hidden : true,
				padding : '0 5px 0 0',
				format : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				minValue : new Date(),
				allowBlank : false,
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			id : 'confirmPurchFloatField2',
			// hidden : true,
			style : 'margin-top:10px;',
			items : [ {
				xtype : 'combo',
				labelWidth : 115,
				fieldLabel : 'Bench Mark',
				store : benchMarkStore,
				displayField : 'name',
				valueField : 'name',
				name : 'benchMark',
				id : 'confirmPurchBench',
				flex : 1.5,
				hidden : true,
				padding : '0 5px 0 0',
				typeAhead : true,
				typeAheadDelay : 100,
				listConfig : {
					loadingText : 'Searching...',
					emptyText : 'No matching posts found.'
				},
			}, {
				xtype : 'numberfield',
				id : 'confirmPurchFloor',
				minValue : 0,
				maxValue : 100,
				decimalPrecision : 4,
				name : 'floorValue',
				labelWidth : 50,
				fieldLabel : 'Floor',
				allowBlank : false,
				flex : 1,
				hidden : true,
				padding : '0 5px 0 0'
			}, {
				xtype : 'numberfield',
				id : 'confirmPurchCeiling',
				minValue : 0,
				maxValue : 100,
				decimalPrecision : 4,
				name : 'ceilingValue',
				allowBlank : false,
				fieldLabel : 'Ceiling',
				labelWidth : 50,
				flex : 1,
				hidden : true,
				padding : '0 5px 0 0'
			} ]
		}, {
			xtype : 'fieldcontainer',
			fieldLabel : 'Withholding Tax',
			defaultType : 'radiofield',
			labelWidth : 115,
			layout : 'hbox',
			padding : '0 5 0 0',
			style : 'margin-top:10px;',
			flex : 1,
			items : [ {
				boxLabel : 'No',
				id : 'confirmPurchWhTNo',
				name : 'withholdingTax',
				inputValue : 'No',
				padding : '0 3 0 0',
				flex : 1,
				checked : true,
				disabled : true,
				listeners : {
					change : function(field, newValue, oldValue) {
						if (newValue) {
							var whtrate = Ext.ComponentQuery.query('#withholdingTaxRate')[0];
							whtrate.setValue("");
							whtrate.disable();
							whtrate.allowBlank = true;
						} else {
							var whtrate = Ext.ComponentQuery.query('#withholdingTaxRate')[0];
							whtrate.setValue("");
							whtrate.enable();
							whtrate.allowBlank = false;
						}
					}
				}
			}, {
				boxLabel : 'Upfront',
				id : 'confirmPurchWhTUp',
				name : 'withholdingTax',
				padding : '0 3 0 0',
				flex : 1,
				inputValue : 'Upfront',
				disabled : true
			}, {
				boxLabel : 'Maturity',
				id : 'confirmPurchWhTMat',
				name : 'withholdingTax',
				padding : '0 3 0 0',
				flex : 1,
				inputValue : 'Maturity',
				disabled : true
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				style : 'margin-top:10px;',
				fieldLabel : 'Withholding Tax Rate',
				flex : 1,
				disabled : true,
				fieldStyle : "text-align:right;",
				maskRe : /[\d%.]/,
				id : 'withholdingTaxRate',
				name : 'withholdingTaxRate',
				padding : '0 10 0 0',
				listeners : {
					blur : function() {
						if (this.getRawValue() <= 100) {
							this.setRawValue(Ext.util.Format.number(this.value, '0.0000%'));
							console.log(Ext.util.Format.number(this.value, '0.0000%'));
						} else {
							this.setRawValue('0.0000%');
						}
					},
					render : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0.0000%'));
					}
				}
			}, new Ext.form.ComboBox({
				store : new Ext.data.Store({
					fields : [ 'dayBasis' ],
					data : [ {
						"dayBasis" : "Act / Act"
					}, {
						"dayBasis" : "Act / 360"
					}, {
						"dayBasis" : "Act / 364"
					}, {
						"dayBasis" : "Act / 365"
					}, {
						"dayBasis" : "30 / 360"
					}, {
						"dayBasis" : "30 / 365"
					} ]
				}),
				displayField : 'dayBasis',
				valueField : 'dayBasis',
				name : 'dayBasis',
				mode : 'local',
				value : 'Act / 365',
				fieldLabel : 'Day Basis',
				forceSelection : true,
				disabled : true,
				labelWidth : 30,
				name : 'dayBasis',
				style : 'margin-top:10px;',
				id : 'confirmPurchDayBasis',
				flex : 1,
				padding : '0 5 0 0',
			}) ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'checkboxfield',
				boxLabel : 'Scripts',
				name : 'scripts',
				inputValue : 1,
				id : 'confirmPurchScripts',
				disabled : true,
				padding : '0 10 0 0',
				style : 'margin-top:10px;',
				flex : 1
			}, {
				xtype : 'checkboxfield',
				boxLabel : 'Denominals',
				name : 'denominals',
				inputValue : 1,
				id : 'confirmPurchdenominals',
				disabled : true,
				padding : '0 10 0 0',
				style : 'margin-top:10px;',
				flex : 1
			}, {
				xtype : 'numberfield',
				minValue : 0,
				decimalPrecision : 4,
				padding : '0 5 0 0',
				style : 'margin-top:10px;',
				flex : 2,
				name : 'denominalMinVal',
				id : 'confirmPurchDenominamMinVal',
				fieldLabel : 'Minimum Value',
				disabled : true,
				allowBlank : false,
			} ]
		}, {
			xtype : 'fieldcontainer',
			fieldLabel : 'Interest Calculation Method',
			defaultType : 'radiofield',
			labelWidth : 115,
			padding : '0 0 5 0',
			style : 'margin-top:10px;',
			layout : 'hbox',
			items : [ {
				boxLabel : 'Straight',
				name : 'intCalMethod',
				id : 'confirmPurchIntCalStr',
				inputValue : 'straight',
				padding : '0 40 0 0',
				disabled : true,
			}, {
				boxLabel : 'Discounted',
				name : 'intCalMethod',
				id : 'confirmPurchIntCalDis',
				inputValue : 'discounted',
				disabled : true,
			} ]
		} ]
	});
};

ConfPurchase.getCenterPanel = function() {

	var basic = ConfPurchase.getBasicFieldSet();
	var instrument = ConfPurchase.getInstrumentFieldSet();
	var deal = ConfPurchase.getDealFieldSet();
	var insDtl = ConfPurchase.getInstrumentDetailFieldSet();
	formPanel = Ext.create('Ext.form.FormPanel', {
		title : 'New Purchase Record',
		id : 'confirmPurchMainPanel',
		bodyPadding : '20',
		autoScroll : true,
		frame : true,
		defaults : {
			anchor : '72%',
		},
		fieldDefaults : {
			labelAlign : 'left',
		},
		items : [ {
			xtype : 'fieldcontainer',
			items : [ {
				xtype : 'triggerfield',
				id : 'confirmPurchTrigField',
				triggerCls : 'x-form-search-trigger',
				anchor : '28%',
				style : 'float:right;',
				emptyText : 'Search for a Purchase',
				listeners : {
					specialkey : function(field, e) {
						if (e.getKey() == e.ENTER) {
							Ext.Msg.alert('HI', 'Press Enter');
						}
					}
				},
				onTriggerClick : function() {
					var searchResult = Ext.create('Ext.data.Store', {
						fields : [ 'id', 'dealCode', 'issuer', 'name', 'status' ]
					});
					var TrigField = Ext.ComponentQuery.query('#confirmPurchTrigField')[0];

					Ext.Ajax.request({
						url : 'getInitPurchases.action',
						params : {
							query : TrigField.getValue(),
						},
						success : function(response) {
							var obj = Ext.decode(response.responseText);
							var arrayLength = obj.deals.length;
							for (var i = 0; i < arrayLength; i++) {
								searchResult.add({
									id : obj.deals[i].id,
									dealCode : obj.deals[i].dealCode,
									issuer : obj.deals[i].issuer.code,
									name : obj.deals[i].name,
									status : obj.deals[i].status,
								});
							}
						}
					});

					var resultWindow = Ext.create('Ext.grid.Panel', {
						defaults : {
							anchor : '100%'
						},
						store : searchResult,
						columns : [ {
							text : 'Deal Code',
							dataIndex : 'dealCode',
							flex : 1
						}, {
							text : 'Issuer',
							dataIndex : 'issuer',
							flex : 1
						}, {
							text : 'Name',
							dataIndex : 'name',
							flex : 2
						}, {
							text : 'Status',
							dataIndex : 'status',
							flex : 1
						} ],

						listeners : {
							itemdblclick : function(view, rec, item, index, eventObj) {
								var findId = rec.get('id');
								var formPanl = Ext.ComponentQuery.query('#confirmPurchMainPanel')[0];
								formPanel.setLoading('Receiving data...', formPanl.getForm());
								var confirmFields = '';
								var rejectBtn = Ext.getCmp('confirmPurchRejectBtn');
								var confirmBtn = Ext.getCmp('confirmPurchConfirmBtn');

								formPanl.getForm().reset();
								formPanl.getForm().getFields().each(function(field) {
									field.setDisabled(true);
								});

								Ext.data.StoreManager.lookup('couponDatesStore').removeAll();

								Ext.Ajax.request({
									url : 'getPurchase.action',
									params : {
										id : findId,
									},
									success : function(response) {
										var obj = Ext.decode(response.responseText);
										var tempInsType = obj.dealDtos.insType;
										var tempInsTypeCmb = Ext.ComponentQuery.query('#confirmPurchInstrumentType')[0];
										var record = tempInsTypeCmb.store.getById(tempInsType);
										tempInsTypeCmb.select(tempInsType);
										tempInsTypeCmb.fireEvent('select', tempInsTypeCmb, record);

										var valDtLocal = Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
										valDtLocal.format = 'd/m/Y';
										valDtLocal.setValue(obj.data.valueDate);

										var matDtLocal = Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];
										matDtLocal.format = 'd/m/Y';
										matDtLocal.setValue(obj.data.maturityDate);

										if (obj.data.resetDate != null) {
											var matDtLocal = Ext.ComponentQuery.query('#confirmPurchFloatResetDt')[0];
											matDtLocal.format = 'd/m/Y';
											matDtLocal.setValue(obj.data.resetDate);
										}

										var localInsStore = Ext.data.StoreManager.lookup('confirmPurchInsStore');
										localInsStore.clearFilter(true);

										var lblDealID = Ext.ComponentQuery.query('#confirmPurchDealID')[0];
										lblDealID.show();
										lblDealID.setText('Deal ID: ' + obj.data.dealCode);

										if (obj.data.params != null) {
											Ext.getCmp('confirmPurchParamLbl').setText(obj.data.params);
										}

										if (!(!obj.data.couponDates || 0 === obj.data.couponDates.length)) {
											var cpnDtsArray = obj.data.couponDates.split(";");
											var cpnDtStoreLocal = Ext.data.StoreManager.lookup('couponDatesStore');
											cpnDtStoreLocal.removeAll();
											var i = 0;
											for (i = 0; i < cpnDtsArray.length - 1; i++) {
												cpnDtStoreLocal.add({
													couponDate : cpnDtsArray[i],
												});
											}
										}

										Ext.Ajax.request({
											url : 'getFundsForDealInsType.action',
											params : {
												querry : 'PURCH',
												querryTwo : tempInsType,
											},
											success : function(response) {
												var obj = Ext.decode(response.responseText);
												Ext.data.StoreManager.lookup('dealFundsStore').loadRawData(obj.fundList);

												formPanl.getForm().load({
													url : 'getPurchase.action',
													params : {
														id : findId,
													},
													success : function(response) {

														if (rec.get('status') === 'Initial') {
															Ext.Ajax.request({
																url : 'confFields.action',
																params : {
																	screen : 'RecordPurchase',
																},
																success : function(response) {
																	var obj = Ext.decode(response.responseText);
																	confirmFields = obj.values;
																	// formPanel.setLoading('Receiving
																	// data...',
																	// formPanl.getForm());

																	formPanl.getForm().getFields().each(function(field) {
																		field.setDisabled(true);
																	});

																	var confFieldsArrray = confirmFields.split(";");
																	var i;
																	var confFieldLocal;
																	var confFieldQuery;
																	for (i = 0; i < confFieldsArrray.length - 1; i++) {
																		confFieldQuery = '#' + confFieldsArrray[i];
																		confFieldLocal = Ext.ComponentQuery.query(confFieldQuery)[0];
																		confFieldLocal.reset();
																		confFieldLocal.enable();
																	}
																	Ext.ComponentQuery.query('#confirmPurchResWindow')[0].close();
																	Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
																	formPanel.setLoading(false);
																},
																failure : function(form, action) {
																	formPanel.setLoading(false);
																	Ext.Msg.alert('Error!', 'Failed to load Confirmation Fields!');
																}
															});
														} else {
															Ext.ComponentQuery.query('#confirmPurchInstrumentType')[0].disable();
															Ext.ComponentQuery.query('#confirmPurchInstrumentCodeCombo')[0].disable();
															Ext.ComponentQuery.query('#confirmPurchInstrumentCodeText')[0].disable();
															Ext.ComponentQuery.query('#confirmPurchFund')[0].disable();
															Ext.ComponentQuery.query('#confirmPurchResWindow')[0].close();
															Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
															formPanel.setLoading(false);
														}
													},
													failure : function(form, action) {
														formPanel.setLoading(false);
														Ext.Msg.alert("An Error occured. Load failed");
													}
												});
											},
											failure : function(form, action) {
												formPanel.setLoading(false);
												Ext.Msg.alert('Error!', 'Failed to load Fund List!');
											}
										});
									},
									failure : function(form, action) {
										formPanel.setLoading(false);
									}
								});

								if (true === false) { // If only viewing
									// permissions
									formPanl.getForm().getFields().each(function(field) {
										field.setDisabled(true);
									});
									confirmBtn.disable();
									rejectBtn.disable();
								} else {
									if (rec.get('status') === 'Initial') {
										confirmBtn.enable();
										rejectBtn.enable();
									} else {
										confirmBtn.disable();
										rejectBtn.disable();
									}
								}
								// this.up('.window').close();
								Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
								// formPanel.setLoading(false);

							}

						}
					});

					Ext.create('Ext.window.Window', {
						title : 'Purchase Deals',
						id : 'confirmPurchResWindow',
						height : 330,
						frame : true,
						width : 500,
						constrain : true,
						resizable : false,
						layout : {
							type : 'fit'
						},
						items : [ resultWindow ]
					}).show();

				}
			}

			]

		}, instrument, basic, deal, insDtl ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
				// Ext.ComponentQuery.query('#confirmPurchRejectBtn')[0].disable();
				// Ext.ComponentQuery.query('#confirmPurchConfirmBtn')[0].disable();
				Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
				Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
				Ext.ComponentQuery.query('#confirmPurchListedLabel')[0].setText('');
				formPanel.getForm().getFields().each(function(field) {
					field.setDisabled(true);
				});
				Ext.getCmp('confirmPurchConfirmBtn').disable();
				Ext.getCmp('confirmPurchRejectBtn').disable();
				Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
			}
		}, {
			text : 'Reject',
			id : 'confirmPurchRejectBtn',
			itemId : 'rejectButton',
			disabled : true,
			handler : function() {
				var form = this.up('form').getForm();
				form.url = 'rejectPurchase.action';
				formPanel.setLoading('Rejecting...');
				formPanel.getForm().getFields().each(function(field) {
					field.setDisabled(true);
				});

				// var rstDt =
				// Ext.ComponentQuery.query('#confirmPurchFloatResetDt')[0];
				// var valueDtField =
				// Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
				// var maturityDtField =
				// Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];
				var dealCode = Ext.ComponentQuery.query('#confirmPurchDealID')[0].text.substr(9);
				console.log('dealCode: ' + dealCode);

				form.submit({
					params : {
						'dealCode' : dealCode,
						'user' : 'updateUser',
					},
					success : function(form, action) {
						form.reset();
						Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
						Ext.Msg.alert('Success', action.result.name + ' is Rejected.<br> Deal Code: <b>' + action.result.dealCode + '</b>');
						Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
						Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
						Ext.ComponentQuery.query('#confirmPurchListedLabel')[0].setText('');
						formPanel.getForm().getFields().each(function(field) {
							field.setDisabled(true);
						});
						Ext.getCmp('confirmPurchConfirmBtn').disable();
						Ext.getCmp('confirmPurchRejectBtn').disable();
						Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
						formPanel.setLoading(false);
					},
					failure : function(form, action) {
						Ext.Msg.alert('Failed', 'There is some error returned from the server.');
						Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
						Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
						Ext.ComponentQuery.query('#confirmPurchListedLabel')[0].setText('');
						formPanel.getForm().getFields().each(function(field) {
							field.setDisabled(true);
						});
						Ext.getCmp('confirmPurchConfirmBtn').disable();
						Ext.getCmp('confirmPurchRejectBtn').disable();
						Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
						formPanel.setLoading(false);
						formPanel.setLoading(false);
					}
				});

			}
		}, {
			text : 'Confirm',
			id : 'confirmPurchConfirmBtn',
			itemId : 'confirmButton',
			disabled : true,
			handler : function() {
				var form = this.up('form').getForm();
				form.url = 'confPurchase.action';

				var whNo = Ext.ComponentQuery.query('#confirmPurchWhTNo')[0].getValue();
				var whUp = Ext.ComponentQuery.query('#confirmPurchWhTUp')[0].getValue();
				var whMat = Ext.ComponentQuery.query('#confirmPurchWhTMat')[0].getValue();

				var tempInsTypeCmb = Ext.ComponentQuery.query('#confirmPurchInstrumentType')[0];
				var tempInsType = tempInsTypeCmb.getValue();
				var record = tempInsTypeCmb.store.getById(tempInsType);
				tempInsTypeCmb.select(tempInsType);
				tempInsTypeCmb.fireEvent('select', tempInsTypeCmb, record);

				Ext.ComponentQuery.query('#confirmPurchFund')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchInsName')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchValueDt')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchInstrumentType')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchDealVal')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchPlacementFee')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchInterestRate')[0].enable();
				Ext.ComponentQuery.query('#withholdingTaxRate')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchDayBasis')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchWhTNo')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchWhTUp')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchWhTMat')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchWhTNo')[0].setValue(whNo);
				Ext.ComponentQuery.query('#confirmPurchWhTUp')[0].setValue(whUp);
				Ext.ComponentQuery.query('#confirmPurchWhTMat')[0].setValue(whMat);

				if (form.isValid()) {
					formPanel.setLoading('Confirming...');
					var couponDates = '';
					Ext.data.StoreManager.lookup('couponDatesStore').each(function(r) {
						couponDates += Ext.util.Format.date(r.get('couponDate')) + ';';
					});
					var rstDt = Ext.ComponentQuery.query('#confirmPurchFloatResetDt')[0];
					var valueDtField = Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
					var maturityDtField = Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];
					var dealCode = Ext.ComponentQuery.query('#confirmPurchDealID')[0].text.substr(9);
					var params = (Ext.ComponentQuery.query('#confirmPurchParamLbl')[0]).text;
					form.submit({
						params : {
							'couponDates' : couponDates,
							'resetDate' : Ext.util.Format.date(rstDt.getValue()),
							'valueDate' : Ext.util.Format.date(valueDtField.getValue()),
							'maturityDate' : Ext.util.Format.date(maturityDtField.getValue()),
							'dealCode' : dealCode,
							'user' : 'confirmedUser',
							'params' : params,
						},
						success : function(form, action) {
							form.reset();
							Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
							Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
							Ext.Msg.alert('Success', action.result.name + ' is confirmed.<br> Deal Code is <b>' + action.result.dealCode + '</b>');
							formPanel.setLoading(false);
							Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
							Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
							Ext.ComponentQuery.query('#confirmPurchListedLabel')[0].setText('');
							formPanel.getForm().getFields().each(function(field) {
								field.setDisabled(true);
							});
							Ext.getCmp('confirmPurchConfirmBtn').disable();
							Ext.getCmp('confirmPurchRejectBtn').disable();
							Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
						},
						failure : function(form, action) {
							form.reset();
							Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
							Ext.Msg.alert('Failed', 'There is some error returned from the server.<br><b>' + action.result.status + '</b>');
							formPanel.setLoading(false);
							Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
							Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
							Ext.ComponentQuery.query('#confirmPurchListedLabel')[0].setText('');
							formPanel.getForm().getFields().each(function(field) {
								field.setDisabled(true);
							});
							Ext.getCmp('confirmPurchConfirmBtn').disable();
							Ext.getCmp('confirmPurchRejectBtn').disable();
							Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
						}
					});
				} else {
					Ext.Msg.alert("ERROR!", "Form Incomplete. Please check!");
					formPanel.getForm().getFields().each(function(field) {
						field.setDisabled(true);
					});
					Ext.Ajax.request({
						url : 'confFields.action',
						params : {
							screen : 'RecordPurchase',
						},
						success : function(response) {
							var obj = Ext.decode(response.responseText);
							confirmFields = obj.values;
							var confFieldsArrray = confirmFields.split(";");
							var i;
							var confFieldLocal;
							var confFieldQuery;
							for (i = 0; i < confFieldsArrray.length - 1; i++) {
								confFieldQuery = '#' + confFieldsArrray[i];
								confFieldLocal = Ext.ComponentQuery.query(confFieldQuery)[0];
								confFieldLocal.enable();
								confFieldLocal.reset();
							}
							Ext.ComponentQuery.query('#confirmPurchResWindow')[0].close();
							Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
						},
						failure : function(form, action) {
							Ext.Msg.alert('Error!', 'Failed to load Confirmation Fields!');
						}
					});
					Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
				}
			}
		} ]
	});
	return formPanel;
};

ConfPurchase.getRollOverDeal = function(instumentType, fund, dealCode, issuer, valueDate, principleValue) {
	var panel = ConfPurchase.getCenterPanel();
	var tmpInsTypeStore = Ext.data.StoreManager.lookup('confirmPurchInsTypeStore');
	var tmpFundStore = Ext.data.StoreManager.lookup('dealFundsStore');
	var tmpPrimeDealerStore = Ext.data.StoreManager.lookup('primaryDealersStore');
	var tmpIssuerStore = Ext.data.StoreManager.lookup('issuersStore');

	var tmpPrimDealerCmb = Ext.getCmp('recordDealPrimDealer');
	var tmpIssuerCmb = Ext.getCmp('recordDealIssuer');
	var insTypeCmb = Ext.getCmp('confirmPurchInstrumentType');
	var fundCmb = Ext.getCmp('confirmPurchFund');
	var tmpValDt = Ext.getCmp('confirmPurchValueDt');
	var tmpDealValue = Ext.getCmp('confirmPurchDealVal');
	var paramLabel = Ext.getCmp('confirmPurchParamLbl');

	Ext.Ajax.request({
		url : 'getInstrumentTypes.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			tmpInsTypeStore.loadRawData(obj.instrumentTypeList);
			insTypeCmb.setValue(tmpInsTypeStore.getAt(tmpInsTypeStore.find('instrumentName', instumentType)).get('id'));
			var insTypeRecord = tmpInsTypeStore.findRecord('instrumentName', instumentType);
			insTypeCmb.fireEvent('select', insTypeCmb, insTypeRecord);
			Ext.Ajax.request({
				url : 'getFundsForDealInsType.action',
				params : {
					querry : 'PURCH',
					querryTwo : insTypeCmb.getValue(),
				},
				success : function(response) {
					var obj = Ext.decode(response.responseText);
					Ext.data.StoreManager.lookup('dealFundsStore').loadRawData(obj.fundList);
					fundCmb.setValue(tmpFundStore.getAt(tmpFundStore.find('fundCode', fund)).get('id'));
					if (!tmpPrimDealerCmb.isDisabled()) {
						tmpPrimDealerCmb.setValue(tmpPrimeDealerStore.getAt(tmpPrimeDealerStore.find('code', issuer)).get('id'));
					} else {
						tmpIssuerCmb.setValue(tmpIssuerStore.getAt(tmpIssuerStore.find('code', issuer)).get('id'));
					}
				},
				failure : function(response) {
					Ext.Msg.alert('Error!', 'Failed to Load Fund!');
				}
			});
		},
		failure : function(response) {
			Ext.Msg.alert('Error!', 'Failed to Load Instrument Type!');
		}
	});

	tmpValDt.setValue(valueDate);
	tmpDealValue.setValue(principleValue);
	paramLabel.setText(dealCode);

	return panel;
};

/*
, {
	xtype : 'fieldcontainer',
	layout : 'hbox',
	items : [ {
		xtype : 'fieldcontainer',
		labelWidth : 115,
		fieldLabel : 'Instrument Type',
		defaultType : 'radiofield',
		flex : 1,
		padding : '0 5px 0 0',
		layout : 'vbox',
		items : [ {
			boxLabel : 'Government',
			padding : '0 40 0 0',
			id : 'cbIssuerGovernment',
			disabled : true
		}, {
			boxLabel : 'Other',
			id : 'cbIssuerOther',
			disabled : true,
		} ]
	}, {
		xtype : 'fieldcontainer',
		layout : 'vbox',
		flex : 1,
		padding : '0 5px 0 0',
		items : [ Ext.create('Ext.form.ComboBox', {
			fieldLabel : 'Primary Dealer',
			store : primaryDealerStore,
			valueField : 'id',
			name : 'primaryDealer',
			queryMode : 'local',
			id : 'recordDealPrimDealer',
			autoScroll : true,
			editable : false,
			disabled : true,
			// flex : 1,
			padding : '0 10px 0 0',
			tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item"><b>{code}</b>-{name}-<b>{creditRating}</b><br/></div>', '</tpl>'),
			displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{name}', '</tpl>'),

		}), Ext.create('Ext.form.ComboBox', {
			fieldLabel : 'Issuer',
			store : issuersStore,
			valueField : 'id',
			name : 'issuer',
			// displayField : 'name',
			queryMode : 'local',
			id : 'recordDealIssuer',
			autoScroll : true,
			editable : false,
			disabled : true,
			padding : '0 10px 0 0',
			tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item"><b>{code}</b>-{name}-<b>{creditRating}</b><br/></div>', '</tpl>'),
			displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{name}', '</tpl>'),
		}) ]
	} ]
}, {
	xtype : 'fieldcontainer',
	layout : 'hbox',
	style : 'margin-top:10px;',
	items : [ {
		xtype : 'fieldcontainer',
		labelWidth : 115,
		fieldLabel : 'Coupon Availability',
		defaultType : 'radiofield',
		id : 'confirmPurchCouponField',
		flex : 1,
		layout : 'hbox',
		items : [ {
			boxLabel : 'No',
			id : 'confirmPurchCpnNo',
			disabled : true
		}, {
			boxLabel : 'Fixed',
			id : 'confirmPurchCpnFixcb',
			disabled : true
		}, {
			boxLabel : 'Vary',
			id : 'confirmPurchCpnVarycb',
			disabled : true
		} ]
	}, {
		xtype : 'numberfield',
		minValue : 0,
		maxValue : 100,
		decimalPrecision : 4,
		flex : 1,
		padding : '0 5px 0 0',
		name : 'couponRate',
		id : 'confirmPurchCouponRate',
		fieldLabel : 'Coupon Rate',
		// *allowBlank : false,
		blankText : 'Coupon Rate is required',
		msgTarget : 'under',
		disabled : true,
	} ]
}, {
	xtype : 'fieldcontainer',
	layout : 'hbox',
	hidden : true,
	id : 'confirmPurchCoupnData',
	style : 'margin-top:10px;',
	items : [ {
		xtype : 'numberfield',
		minValue : 0,
		labelWidth : 115,
		maxValue : 100,
		decimalPrecision : 0,
		// padding : '0 0 5 0',
		name : 'couponFreq',
		anchor : '60%',
		id : 'confirmPurchCouponFreq',
		fieldLabel : 'Coupon Frequency',
		disabled : true,
		hidden : true,
		allowBlank : false,
	}, {
		xtype : 'datefield',
		fieldLabel : 'Coupon Dates',
		id : 'confirmPurchCpnDts',
		minValue : new Date(),
		flex : 1,
		padding : '0 5px 0 0',
		labelWidth : 115,
		allowBlank : false,
		blankText : 'Please select a Start Date',
		msgTarget : 'under',
		format : 'd/m/Y',
		emptyText : 'DD/MM/YYYY',
		hidden : true,
		listeners : {
			change : {
				fn : function(object, newVal, oldVal) {

					var recordIndex = cpnDtStore.findBy(function(record, id) {
						if (newVal != null && (record.get('couponDate').getTime() == newVal.getTime())) {
							return true;
						}
						return false;
					});
					if (recordIndex != -1) {
						Ext.MessageBox.alert('Error!', 'Duplicate Entry!!!');
					} else if (newVal == null) {
						console.log("Null value for date field");
					} else if (valDate.getValue() == null || matDate.getValue() == null) {
						Ext.MessageBox.alert('Error!', 'Deal Period not specified!');
					} else if (newVal < valDate.getValue() || newVal > matDate.getValue()) {
						Ext.MessageBox.alert('Error!', 'Coupon Date should be within the Deal Period!');
					} else {
						cpnDtStore.add({
							couponDate : Ext.util.Format.date(newVal, 'd/m/Y')
						});
					}
				}
			}
		}
	}, Ext.create('Ext.grid.Panel', {
		width : 100,
		height : 130,
		flex : 1,
		store : cpnDtStore,
		id : 'recordDealDtStore',
		autoScroll : true,
		columns : [ {
			xtype : 'datecolumn',
			header : 'Coupon Dates',
			dataIndex : 'couponDate',
			format : 'd/m/Y',
			flex : 1
		} ],
		hidden : true,
		bbar : [ {
			xtype : 'button',
			text : 'Remove',
			handler : function() {
				var pCpnDtStore = Ext.ComponentQuery.query('#recordDealDtStore')[0];
				var sm = pCpnDtStore.getSelectionModel();
				var sel = sm.getSelection();
				var i = 0;
				var currentDate = new Date();
				for (i = 0; i < sel.length; i++) {
					if (sel[i].get("couponDate") > currentDate) {
						pCpnDtStore.store.remove(sel[i]);
					} else {
						Ext.Msg.alert('Error', 'Cannot Remove Previous Dates!');
					}
				}
			}
		} ]
	}) ]
}, {
	xtype : 'fieldcontainer',
	layout : 'hbox',
	items : [ {
		xtype : 'fieldcontainer',
		fieldLabel : 'Interest Type',
		defaultType : 'radiofield',
		layout : 'hbox',
		flex : 1,
		labelWidth : 115,
		style : 'margin-top:10px;',
		items : [ {
			boxLabel : 'Fixed',
			id : 'confirmPurchIntTypeFix',
			disabled : true
		}, {
			boxLabel : 'Floating',
			id : 'confirmPurchIntTypeFloat',
			disabled : true
		} ]
	}, {
		xtype : 'numberfield',
		minValue : 0,
		maxValue : 100,
		decimalPrecision : 4,
		padding : '0 5px 0 0',
		name : 'interestRate',
		fieldLabel : 'Rate',
		style : 'margin-top:10px;',
		flex : 1,
		id : 'confirmPurchInterestRate',
		allowBlank : false,
		blankText : 'Rate is required',
		msgTarget : 'under',
		disabled : true,
	} ]
}, {
	xtype : 'fieldcontainer',
	layout : 'hbox',
	id : 'confirmPurchFloatField1',
	// hidden : true,

	items : [ {
		xtype : 'numberfield',
		id : 'RecPurchResetPeriod',
		labelWidth : 115,
		fieldLabel : 'Reset Period',
		name : 'rateResetPeriod',
		flex : 1,
		hidden : true,
		minValue : 1,
		decimalPrecision : 0,
		allowBlank : false,
		padding : '0 79px 0 0'
	}, {
		xtype : 'datefield',
		fieldLabel : 'Reset Date',
		id : 'confirmPurchFloatResetDt',
		flex : 1,
		hidden : true,
		padding : '0 5px 0 0',
		format : 'd/m/Y',
		emptyText : 'DD/MM/YYYY',
		minValue : new Date(),
		allowBlank : false,
	} ]
}, {
	xtype : 'fieldcontainer',
	layout : 'hbox',
	id : 'confirmPurchFloatField2',
	// hidden : true,
	style : 'margin-top:10px;',
	items : [ {
		xtype : 'combo',
		labelWidth : 115,
		fieldLabel : 'Bench Mark',
		store : benchMarkStore,
		displayField : 'name',
		valueField : 'name',
		name : 'benchMark',
		id : 'confirmPurchBench',
		flex : 1.5,
		hidden : true,
		padding : '0 5px 0 0',
		typeAhead : true,
		typeAheadDelay : 100,
		listConfig : {
			loadingText : 'Searching...',
			emptyText : 'No matching posts found.'
		},
	}, {
		xtype : 'numberfield',
		id : 'confirmPurchFloor',
		minValue : 0,
		maxValue : 100,
		decimalPrecision : 4,
		name : 'floorValue',
		labelWidth : 50,
		fieldLabel : 'Floor',
		allowBlank : false,
		flex : 1,
		hidden : true,
		padding : '0 5px 0 0'
	}, {
		xtype : 'numberfield',
		id : 'confirmPurchCeiling',
		minValue : 0,
		maxValue : 100,
		decimalPrecision : 4,
		name : 'ceilingValue',
		allowBlank : false,
		fieldLabel : 'Ceiling',
		labelWidth : 50,
		flex : 1,
		hidden : true,
		padding : '0 5px 0 0'
	} ]
}, {
	xtype : 'fieldcontainer',
	fieldLabel : 'Withholding Tax',
	defaultType : 'radiofield',
	labelWidth : 115,
	layout : 'hbox',
	padding : '0 5 0 0',
	style : 'margin-top:10px;',
	flex : 1,
	items : [ {
		boxLabel : 'No',
		id : 'confirmPurchWhTNo',
		name : 'withholdingTax',
		inputValue : 'No',
		padding : '0 3 0 0',
		flex : 1,
		checked : true,
		disabled : true,
		listeners : {
			change : function(field, newValue, oldValue) {
				if (newValue) {
					var whtrate = Ext.ComponentQuery.query('#withholdingTaxRate')[0];
					whtrate.setValue("");
					whtrate.disable();
					whtrate.allowBlank = true;
				} else {
					var whtrate = Ext.ComponentQuery.query('#withholdingTaxRate')[0];
					whtrate.setValue("");
					whtrate.enable();
					whtrate.allowBlank = false;
				}
			}
		}
	}, {
		boxLabel : 'Upfront',
		id : 'confirmPurchWhTUp',
		name : 'withholdingTax',
		padding : '0 3 0 0',
		flex : 1,
		inputValue : 'Upfront',
		disabled : true
	}, {
		boxLabel : 'Maturity',
		id : 'confirmPurchWhTMat',
		name : 'withholdingTax',
		padding : '0 3 0 0',
		flex : 1,
		inputValue : 'Maturity',
		disabled : true
	} ]
}, {
	xtype : 'fieldcontainer',
	layout : 'hbox',
	items : [ {
		xtype : 'textfield',
		style : 'margin-top:10px;',
		fieldLabel : 'Withholding Tax Rate',
		flex : 1,
		disabled : true,
		fieldStyle : "text-align:right;",
		maskRe : /[\d%.]/,
		id : 'withholdingTaxRate',
		name : 'withholdingTaxRate',
		padding : '0 10 0 0',
		listeners : {
			blur : function() {
				if (this.getRawValue() <= 100) {
					this.setRawValue(Ext.util.Format.number(this.value, '0.0000%'));
					console.log(Ext.util.Format.number(this.value, '0.0000%'));
				} else {
					this.setRawValue('0.0000%');
				}
			},
			render : function() {
				this.setRawValue(Ext.util.Format.number(this.value, '0.0000%'));
			}
		}
	}, new Ext.form.ComboBox({
		store : new Ext.data.Store({
			fields : [ 'dayBasis' ],
			data : [ {
				"dayBasis" : "Act / Act"
			}, {
				"dayBasis" : "Act / 360"
			}, {
				"dayBasis" : "Act / 364"
			}, {
				"dayBasis" : "Act / 365"
			}, {
				"dayBasis" : "30 / 360"
			}, {
				"dayBasis" : "30 / 365"
			} ]
		}),
		displayField : 'dayBasis',
		valueField : 'dayBasis',
		name : 'dayBasis',
		mode : 'local',
		value : 'Act / 365',
		fieldLabel : 'Day Basis',
		forceSelection : true,
		disabled : true,
		labelWidth : 30,
		name : 'dayBasis',
		style : 'margin-top:10px;',
		id : 'confirmPurchDayBasis',
		flex : 1,
		padding : '0 5 0 0',
	}) ]
}, {
	xtype : 'fieldcontainer',
	layout : 'hbox',
	items : [ {
		xtype : 'checkboxfield',
		boxLabel : 'Scripts',
		name : 'scripts',
		inputValue : 1,
		id : 'confirmPurchScripts',
		disabled : true,
		padding : '0 10 0 0',
		style : 'margin-top:10px;',
		flex : 1
	}, {
		xtype : 'checkboxfield',
		boxLabel : 'Denominals',
		name : 'denominals',
		inputValue : 1,
		id : 'confirmPurchdenominals',
		disabled : true,
		padding : '0 10 0 0',
		style : 'margin-top:10px;',
		flex : 1
	}, {
		xtype : 'numberfield',
		minValue : 0,
		decimalPrecision : 4,
		padding : '0 5 0 0',
		style : 'margin-top:10px;',
		flex : 2,
		name : 'denominalMinVal',
		id : 'confirmPurchDenominamMinVal',
		fieldLabel : 'Minimum Value',
		disabled : true,
		allowBlank : false,
	} ]
}, {
	xtype : 'fieldcontainer',
	fieldLabel : 'Interest Calculation Method',
	defaultType : 'radiofield',
	labelWidth : 115,
	padding : '0 0 5 0',
	style : 'margin-top:10px;',
	layout : 'hbox',
	items : [ {
		boxLabel : 'Straight',
		name : 'intCalMethod',
		id : 'confirmPurchIntCalStr',
		inputValue : 'straight',
		padding : '0 40 0 0',
		disabled : true,
	}, {
		boxLabel : 'Discounted',
		name : 'intCalMethod',
		id : 'confirmPurchIntCalDis',
		inputValue : 'discounted',
		disabled : true,
	} ]
} */